diff --git a/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.Max2SATAnsatz.rst b/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.Max2SATAnsatz.rst index c5e8633e2e401cb245612033dd145bc8ef36e84b..b05c3893cd0b3481c0607586f9902af8c43d33bd 100644 --- a/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.Max2SATAnsatz.rst +++ b/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.Max2SATAnsatz.rst @@ -7,8 +7,8 @@ mindquantum.algorithm.nisq.Max2SATAnsatz .. math:: - U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} - \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n} + 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} .. math:: diff --git a/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.MaxCutAnsatz.rst b/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.MaxCutAnsatz.rst index 49d8632690de9b939b2df975350b28b6fbfe71b1..335c168cdc1a0fa19a16ae5fc43e3e52cd139281 100644 --- a/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.MaxCutAnsatz.rst +++ b/docs/api_python/algorithm/nisq/mindquantum.algorithm.nisq.MaxCutAnsatz.rst @@ -7,8 +7,8 @@ mindquantum.algorithm.nisq.MaxCutAnsatz .. math:: - U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} - \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n} + 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} .. math:: diff --git a/mindquantum/algorithm/nisq/qaoa/max_2_sat_ansatz.py b/mindquantum/algorithm/nisq/qaoa/max_2_sat_ansatz.py index 82b96dea4ab617786e2a613900607fecb22834ee..bf9b812962e2d801d62af69a16890e3ea89bf0d9 100644 --- a/mindquantum/algorithm/nisq/qaoa/max_2_sat_ansatz.py +++ b/mindquantum/algorithm/nisq/qaoa/max_2_sat_ansatz.py @@ -70,8 +70,8 @@ class Max2SATAnsatz(Ansatz): .. math:: - U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} - \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n} + 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, @@ -95,12 +95,12 @@ class Max2SATAnsatz(Ansatz): >>> clauses = [(2, -3)] >>> max2sat = Max2SATAnsatz(clauses, 1) >>> max2sat.circuit - ┏━━━┓ ┏━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━┓ - q1: ──┨ H ┠─┨ RZ(1/2*beta_0) ┠────■─────────────────────────■───┨ RX(alpha_0) ┠─── - ┗━━━┛ ┗━━━━━━━━━━━━━━━━┛ ┃ ┃ ┗━━━━━━━━━━━━━┛ - ┏━━━┓ ┏━━━━━━━━━━━━━━━━━┓ ┏━┻━┓ ┏━━━━━━━━━━━━━━━━━┓ ┏━┻━┓ ┏━━━━━━━━━━━━━┓ - q2: ──┨ H ┠─┨ RZ(-1/2*beta_0) ┠─┨╺╋╸┠─┨ RZ(-1/2*beta_0) ┠─┨╺╋╸┠─┨ RX(alpha_0) ┠─── - ┗━━━┛ ┗━━━━━━━━━━━━━━━━━┛ ┗━━━┛ ┗━━━━━━━━━━━━━━━━━┛ ┗━━━┛ ┗━━━━━━━━━━━━━┛ + ┏━━━┓ ┏━━━━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━┓ + 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] + @@ -133,19 +133,19 @@ class Max2SATAnsatz(Ansatz): ham = QubitOperator() for clause in clauses: ham += ( - sign(1, clause[0]) * QubitOperator(f'Z{abs(clause[0]) - 1}', 'beta') - + sign(1, clause[1]) * QubitOperator(f'Z{abs(clause[1]) - 1}', 'beta') + sign(1, clause[0]) * QubitOperator(f'Z{abs(clause[0]) - 1}', 'gamma') + + sign(1, clause[1]) * QubitOperator(f'Z{abs(clause[1]) - 1}', 'gamma') ) / 4 ham += ( sign(1, clause[0]) * sign(1, clause[1]) - * QubitOperator(f'Z{abs(clause[0]) - 1} Z{abs(clause[1]) - 1}', 'beta') + * QubitOperator(f'Z{abs(clause[0]) - 1} Z{abs(clause[1]) - 1}', 'gamma') ) / 4 return TimeEvolution(ham.real).circuit def _build_hb(self, clauses): """Build hb circuit.""" - return Circuit([RX('alpha').on(i) for i in _get_clause_act_qubits(clauses)]) + return Circuit([RX('beta').on(i) for i in _get_clause_act_qubits(clauses)]) @property def hamiltonian(self): @@ -206,5 +206,5 @@ class Max2SATAnsatz(Ansatz): """Implement of Max-2-SAT ansatz.""" self._circuit = UN(H, _get_clause_act_qubits(clauses)) for depth_idx in range(depth): - self._circuit += CPN(self._build_hc(clauses), {'beta': f'beta_{depth_idx}'}) - self._circuit += CPN(self._build_hb(clauses), {'alpha': f'alpha_{depth_idx}'}) + self._circuit += CPN(self._build_hc(clauses), {'gamma': f'gamma_{depth_idx}'}) + self._circuit += CPN(self._build_hb(clauses), {'beta': f'beta_{depth_idx}'}) diff --git a/mindquantum/algorithm/nisq/qaoa/max_cut_ansatz.py b/mindquantum/algorithm/nisq/qaoa/max_cut_ansatz.py index cd20a059cc138aedf246d54a55c2dded9ff5da8c..1f73d601129334371a3e9f2b404855376b19dd19 100644 --- a/mindquantum/algorithm/nisq/qaoa/max_cut_ansatz.py +++ b/mindquantum/algorithm/nisq/qaoa/max_cut_ansatz.py @@ -66,8 +66,8 @@ class MaxCutAnsatz(Ansatz): .. math:: - U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} - \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n} + 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, @@ -91,15 +91,15 @@ class MaxCutAnsatz(Ansatz): >>> 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) ┠─── - ┗━━━┛ ┗━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━┛ + ┏━━━┓ ┏━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━┓ + q0: ──┨ H ┠─┨ ┠──────────────────● ┠─┨ RX(beta_0) ┠─── + ┗━━━┛ ┃ ┃ ┃ ┃ ┗━━━━━━━━━━━━┛ + ┏━━━┓ ┃ Rzz(gamma_0) ┃ ┏━━━━━━━━━━━━━━┓ ┃ ┃ ┏━━━━━━━━━━━━┓ + q1: ──┨ H ┠─┨ ┠─┨ ┠─┨ Rzz(gamma_0) ┠─┨ RX(beta_0) ┠─── + ┗━━━┛ ┗━━━━━━━━━━━━━━┛ ┃ ┃ ┃ ┃ ┗━━━━━━━━━━━━┛ + ┏━━━┓ ┃ Rzz(gamma_0) ┃ ┃ ┃ ┏━━━━━━━━━━━━┓ + q2: ──┨ H ┠──────────────────┨ ┠─● ┠─┨ RX(beta_0) ┠─── + ┗━━━┛ ┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━┛ >>> >>> print(maxcut.hamiltonian) 3/2 [] + @@ -133,12 +133,12 @@ class MaxCutAnsatz(Ansatz): """Build hc circuit.""" circ = Circuit() for node in graph: - circ += Rzz('beta').on(node) + circ += Rzz('gamma').on(node) return circ def _build_hb(self, graph): """Build hb circuit.""" - return Circuit([RX('alpha').on(i) for i in _get_graph_act_qubits(graph)]) + return Circuit([RX('beta').on(i) for i in _get_graph_act_qubits(graph)]) @property def hamiltonian(self): @@ -228,5 +228,5 @@ class MaxCutAnsatz(Ansatz): """Implement of max cut ansatz.""" self._circuit = UN(H, _get_graph_act_qubits(graph)) for current_depth in range(depth): - self._circuit += CPN(self._build_hc(graph), {'beta': f'beta_{current_depth}'}) - self._circuit += CPN(self._build_hb(graph), {'alpha': f'alpha_{current_depth}'}) + self._circuit += CPN(self._build_hc(graph), {'gamma': f'gamma_{current_depth}'}) + self._circuit += CPN(self._build_hb(graph), {'beta': f'beta_{current_depth}'}) diff --git a/mindquantum/algorithm/nisq/qaoa/qaoa_ansatz.py b/mindquantum/algorithm/nisq/qaoa/qaoa_ansatz.py index c3d219043aab66cd2b3f23e5334ff82710a7f74c..ee7f3f9c9ceb2a441836d553aab180db6ef42726 100644 --- a/mindquantum/algorithm/nisq/qaoa/qaoa_ansatz.py +++ b/mindquantum/algorithm/nisq/qaoa/qaoa_ansatz.py @@ -43,8 +43,8 @@ class QAOAAnsatz(Ansatz): .. math:: - U(\beta, \gamma) = e^{-\beta_pH_b}e^{-\gamma_pH_c} - \cdots e^{-\beta_0H_b}e^{-\gamma_0H_c}H^{\otimes n} + 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} Args: ham (QubitOperator): The hamiltonian structure. @@ -56,11 +56,15 @@ class QAOAAnsatz(Ansatz): >>> ham = QubitOperator('Z0 Z1', 2) + QubitOperator('Z2 Z1', 1) + QubitOperator('Z0 Y2', 0.5) >>> qaoa = QAOAAnsatz(ham, 1) >>> qaoa.circuit[:11] - q0: ──H────●────────────────────●── - │ │ - q1: ──H────X────RZ(4*beta_0)────X── - - q2: ──H──────────────────────────── + ┏━━━┓ + q0: ──┨ H ┠───■───────────────────────■───── + ┗━━━┛ ┃ ┃ + ┏━━━┓ ┏━┻━┓ ┏━━━━━━━━━━━━━━━┓ ┏━┻━┓ + q1: ──┨ H ┠─┨╺╋╸┠─┨ RZ(4*gamma_0) ┠─┨╺╋╸┠─── + ┗━━━┛ ┗━━━┛ ┗━━━━━━━━━━━━━━━┛ ┗━━━┛ + ┏━━━┓ + q2: ──┨ H ┠───────────────────────────────── + ┗━━━┛ >>> qaoa.hamiltonian 2 [Z0 Z1] + 0.5 [Z0 Y2] + @@ -92,12 +96,12 @@ class QAOAAnsatz(Ansatz): circ = Circuit() for h in ham.terms: if h: - circ += decompose_single_term_time_evolution(h, {'beta': ham.terms[h]}) + circ += decompose_single_term_time_evolution(h, {'gamma': ham.terms[h].const}) return circ def _build_hb(self, circ): """Build hc circuit.""" - return Circuit([RX('alpha').on(i) for i in circ.all_qubits.keys()]) + return Circuit([RX('beta').on(i) for i in circ.all_qubits.keys()]) def _implement(self, ham, depth): # pylint: disable=arguments-differ """Implement of QAOA ansatz.""" @@ -105,5 +109,5 @@ class QAOAAnsatz(Ansatz): hb = self._build_hb(hc) self._circuit = UN(H, hc.all_qubits.keys()) for current_depth in range(depth): - self._circuit += CPN(hc, {'beta': f'beta_{current_depth}'}) - self._circuit += CPN(hb, {'alpha': f'alpha_{current_depth}'}) + self._circuit += CPN(hc, {'gamma': f'gamma_{current_depth}'}) + self._circuit += CPN(hb, {'beta': f'beta_{current_depth}'}) diff --git a/mindquantum/algorithm/nisq/qaoa/rqaoa_ansatz.py b/mindquantum/algorithm/nisq/qaoa/rqaoa_ansatz.py index ff363cfbbe2cbe545828c0b88da15b3ef9b230cb..235df17f9548ed4e7c309a67fef6f4567671d857 100644 --- a/mindquantum/algorithm/nisq/qaoa/rqaoa_ansatz.py +++ b/mindquantum/algorithm/nisq/qaoa/rqaoa_ansatz.py @@ -97,7 +97,7 @@ class RQAOAAnsatz(QAOAAnsatz): >>> ra.translate({(0, 'Z'):-1, (1, 'Z'):1}) # recover original problem's solution by the subproblem's solution {(0, 'Z'): -1, (1, 'Z'): 1, (2, 'Z'): -1} >>> ra = RQAOAAnsatz(ham, 1) - >>> pr = {'beta_0': -0.4617199, 'alpha_0': 0.6284928} + >>> pr = {'gamma_0': -0.4617199, 'beta_0': 0.6284928} >>> ra.eliminate_variable(pr, 1) # reduce variable -- eliminated variable: Z1 -- correlated variable: Z0