mindquantum.algorithm.nisq.MaxCutAnsatz

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

The MaxCut ansatz.

For more detail, please refer to A Quantum Approximate Optimization Algorithm.

\[U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n}\]

Where,

\[H_b = \sum_{i\in n}X_{i}, H_c = \sum_{(i,j)\in C}Z_iZ_j\]

Here \(n\) is the set of nodes and \(C\) is the set of edges of the graph.

Parameters
  • graph (list[tuple[int]]) – The graph structure. Every element of graph is a edge that constructed by two nodes. For example, [(0, 1), (1, 2)] means the graph has three nodes which are 0 , 1 and 2 with one edge connect between node 0 and node 1 and another connect between node 1 and node 2.

  • depth (int) – The depth of max cut ansatz. Default: 1.

Examples

>>> import numpy as np
>>> from mindquantum.algorithm.nisq import MaxCutAnsatz
>>> graph = [(0, 1), (1, 2), (0, 2)]
>>> maxcut = MaxCutAnsatz(graph, 1)
>>> maxcut.circuit
      ┏━━━┓ ┏━━━━━━━━━━━━━┓                 ┏━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━┓
q0: ──┨ H ┠─┨             ┠─────────────────●             ┠─┨ RX(alpha_0) ┠───
      ┗━━━┛ ┃             ┃                 ┃             ┃ ┗━━━━━━━━━━━━━┛
      ┏━━━┓ ┃ Rzz(beta_0) ┃ ┏━━━━━━━━━━━━━┓ ┃             ┃ ┏━━━━━━━━━━━━━┓
q1: ──┨ H ┠─┨             ┠─┨             ┠─┨ Rzz(beta_0) ┠─┨ RX(alpha_0) ┠───
      ┗━━━┛ ┗━━━━━━━━━━━━━┛ ┃             ┃ ┃             ┃ ┗━━━━━━━━━━━━━┛
      ┏━━━┓                 ┃ Rzz(beta_0) ┃ ┃             ┃ ┏━━━━━━━━━━━━━┓
q2: ──┨ H ┠─────────────────┨             ┠─●             ┠─┨ RX(alpha_0) ┠───
      ┗━━━┛                 ┗━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛
>>>
>>> print(maxcut.hamiltonian)
3/2 [] +
-1/2 [Z0 Z1] +
-1/2 [Z0 Z2] +
-1/2 [Z1 Z2]
>>> partitions = maxcut.get_partition(5, np.array([4, 1]))
>>> for i in partitions:
...     print(f'partition: left: {i[0]}, right: {i[1]}, cut value: {maxcut.get_cut_value(i)}')
partition: left: [2], right: [0, 1], cut value: 2
partition: left: [0, 1], right: [2], cut value: 2
partition: left: [0], right: [1, 2], cut value: 2
partition: left: [0, 1, 2], right: [], cut value: 0
partition: left: [], right: [0, 1, 2], cut value: 0
get_cut_value(partition)[source]

Get the cut values for given partitions.

The partition is a list that contains two lists, each list contains the nodes of the given graph.

Parameters

partition (list) – a partition of the graph considered.

Returns

int, cut_value under the given partition.

get_partition(max_n, weight)[source]

Get the partitions of this max-cut problem.

Parameters
Returns

list, a list of partitions.

property hamiltonian

Get the hamiltonian of this max cut problem.

Returns

QubitOperator, hamiltonian of this max cut problem.