# Qiskit: QAOAnsatz circuit with custom Hamiltonian

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?

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


Finally,

ansatz = QAOAAnsatz(cost_operator)

• 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? Commented Apr 27, 2022 at 8:12
• As it seems the t parameters are present only in the transpiled circuit. Commented Apr 27, 2022 at 9:44