I am trying to implement the Quantum Approximate Optimization Ansatz by creating a parametrized subcircuit

$$V (α) = e^{−iH_M α_1} e^{−iH_D b_1} ... e^{−iH_M α_n} e^{−iH_D b_n}$$ with the custom driver hamiltonian $H_M = \mathbb{I} - \left|b \right> \left< b\right|$, where $\left| b \right>= U \left| 0 \right>$ is a random normalized state, and the default $H_M$ mixer hamiltonian of the original paper.

I have a problem feeding my hamiltonian to the QAOAAnsatz as it asks an OperatorBase class for input.

How do I construct the Operator object for QAOAAnsatz or how do I create a custom QAOA circuit?


1 Answer 1


Assume that $U$ is given as a quantum circuit:

U = QuantumCircuit(num_qubits)

Then to get the state vector $\left| b \right>$ we can use Statevector.evolve() method[1]

from qiskit.quantum_info import Statevector

zero = Statevector.from_label('0'*num_qubits)
b = zero.evolve(U)

The method Statevector.to_operator()[2] converts a state to a rank-1 projector operator. So we can construct an OperatorBase instance for $H_D = \mathbb{I} - \left|b \right> \left< b\right|$ as follows:

from qiskit.opflow import I
from qiskit.opflow.primitive_ops import PrimitiveOp

projector_op = PrimitiveOp(b.to_operator())
cost_operator = (I^num_qubits) - projector_op


ansatz = QAOAAnsatz(cost_operator)
  • $\begingroup$ Thank you, seems to work so far. I have a question though: when I do ansatz.parameters it returns a list of 2n=4 parameters as expected (n=2 in my case). But when I decompose and draw the circuit two extra parameters are shown: -0.875(t[0]+t[2]) with the indication of Global Phase. When I use the QAOA as a subcircuit to qc and do qc.parameters I get indeed 6 parameters instead of 4. What is going on? $\endgroup$
    – noobier
    Apr 27 at 8:12
  • $\begingroup$ As it seems the t parameters are present only in the transpiled circuit. $\endgroup$
    – noobier
    Apr 27 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.