mindquantum.algorithm.nisq.Max2SATAnsatz

View Source On Gitee
class mindquantum.algorithm.nisq.Max2SATAnsatz(clauses, depth=1)[source]

The Max-2-SAT ansatz.

For more detail, please refer to Reachability Deficits in Quantum Approximate Optimization.

\[U(\beta, \gamma) = e^{-i\beta_pH_b}e^{-i\frac{\gamma_p}{2}H_c} \cdots e^{-i\beta_0H_b}e^{-i\frac{\gamma_0}{2}H_c}H^{\otimes n}\]

Where,

\[H_b = \sum_{i\in n}X_{i}, H_c = \sum_{l\in m}P(l)\]

Here \(n\) is the number of Boolean variables and \(m\) is the number of total clauses and \(P(l)\) is rank-one projector.

Parameters
  • clauses (list[tuple[int]]) – The Max-2-SAT structure. Every element of list is a clause represented by a tuple with length two. The element of tuple must be non-zero integer. For example, (2, -3) stands for clause \(x_2\lor\lnot x_3\).

  • depth (int) – The depth of Max-2-SAT ansatz. Default: 1.

Examples

>>> import numpy as np
>>> from mindquantum.algorithm.nisq import Max2SATAnsatz
>>> clauses = [(2, -3)]
>>> max2sat = Max2SATAnsatz(clauses, 1)
>>> max2sat.circuit
      ┏━━━┓ ┏━━━━━━━━━━━━━━━━━┓                                   ┏━━━━━━━━━━━━┓
q1: ──┨ H ┠─┨ RZ(1/2*gamma_0) ┠────■──────────────────────────■───┨ RX(beta_0) ┠───
      ┗━━━┛ ┗━━━━━━━━━━━━━━━━━┛    ┃                          ┃   ┗━━━━━━━━━━━━┛
      ┏━━━┓ ┏━━━━━━━━━━━━━━━━━━┓ ┏━┻━┓ ┏━━━━━━━━━━━━━━━━━━┓ ┏━┻━┓ ┏━━━━━━━━━━━━┓
q2: ──┨ H ┠─┨ RZ(-1/2*gamma_0) ┠─┨╺╋╸┠─┨ RZ(-1/2*gamma_0) ┠─┨╺╋╸┠─┨ RX(beta_0) ┠───
      ┗━━━┛ ┗━━━━━━━━━━━━━━━━━━┛ ┗━━━┛ ┗━━━━━━━━━━━━━━━━━━┛ ┗━━━┛ ┗━━━━━━━━━━━━┛
>>> max2sat.hamiltonian
1/4 [] +
1/4 [Z1] +
-1/4 [Z1 Z2] +
-1/4 [Z2]
>>> sats = max2sat.get_sat(4, np.array([4, 1]))
>>> sats
['001', '000', '011', '010']
>>> for i in sats:
...     print(f'sat value: {max2sat.get_sat_value(i)}')
sat value: 1
sat value: 0
sat value: 2
sat value: 1
get_sat(max_n, weight)[source]

Get the strings of this max-2-sat problem.

Parameters
Returns

list, a list of strings.

get_sat_value(string)[source]

Get the sat values for given strings.

The string is a str that satisfies all the clauses of the given max-2-sat problem.

Parameters

string (str) – a string of the max-2-sat problem considered.

Returns

int, sat_value under the given string.

property hamiltonian

Get the hamiltonian of this max-2-sat problem.

Returns

QubitOperator, hamiltonian of this max-2-sat problem.