3
$\begingroup$

I am having some trouble running the compute_minimum_eigenvalue method from qiskit's QAOA: the documentation states that one need to pass H (" Qubit operator of the Observable" as OperatorBase). My question is, given that I implemented H as QuantumCircuit, how can I convert it to the right format? Thanks a lot!

$\endgroup$
1
  • $\begingroup$ You can create a quantum_info/Operator from the QuantumCircuit and with that operator create an opflow/MatrixOp, you can try that. There is a more direct route using opflow/CircuitOp but I think the former is more likely to work. Normally the operator there is a pauli sum from a higher level application, but the above will convert it to the right type. $\endgroup$
    – Steve Wood
    Commented May 20, 2022 at 15:19

2 Answers 2

2
$\begingroup$

I had suggested in my comment above how you might do this. Operator is not an opflow OperatorBase (it's a BaseOperator - different type) but can be used to create an opflow MatrixOp which is. Given your code sample above, I did the following based on it, which runs and prints values.

import numpy as np
from qiskit import BasicAer, QuantumCircuit
from qiskit.algorithms import QAOA
from qiskit.algorithms.optimizers import COBYLA
from qiskit.quantum_info.operators import Operator
from qiskit.opflow import MatrixOp

nqubits = 4
H = QuantumCircuit(nqubits)
for i in range(nqubits):
    H.z(i)
    H_op = MatrixOp(Operator(H))
    qaoa = QAOA(optimizer= COBYLA(), reps=1, mixer=H,
                initial_point=np.array([1.0]),
                quantum_instance=BasicAer.get_backend('statevector_simulator'))
    print(qaoa.compute_minimum_eigenvalue(H_op))

Note: you should use an optimizer from the qiskit.algorithms.optimizers package - the qiskit.optimization is part of the now deprecated and unsupported Qiskit Aqua, and that specific module you had used (CobylaOptimizer), in any case, had a different purpose.

$\endgroup$
1
  • $\begingroup$ Thanks a lot, I have an additional question: when I want to see the circuit I do qaoa.construct_circuit([1.], H_op)[0].draw(), and I have noticed something odd: there is a block that contains all 4 qubits that says "Hamiltonian". Do you know what it is and what it does? Because from what I could understand until now, there is an additional parameter that goes into this Hamiltonian, but I don't see why it should be there. $\endgroup$ Commented May 24, 2022 at 14:53
1
$\begingroup$

I'm not sure if I'm interpreting the scope of the question correctly, but you can convert a QuantumCircuit to a compatible OperatorBase type for compute_minimum_eigenvalue as follows:

from qiskit import QuantumCircuit
from qiskit.algorithms import QAOA
from qiskit.opflow import MatrixOp
from qiskit.quantum_info.operators import Operator

circuit = QuantumCircuit(1)
circuit.h(0)

op = MatrixOp(Operator(circuit)) # convert circuit to operator

qaoa = QAOA(...) # specifications of your problem

qaoa.compute_minimum_eigenvalue(op, ...)
$\endgroup$
2
  • $\begingroup$ Thanks a lot! But if I try to run this with a simple QAOA I get a strange error: qiskit.exceptions.QiskitError: 'other is not a number'. My code is: from qiskit.algorithms import QAOA from qiskit.optimization.algorithms import CobylaOptimizer from qiskit.quantum_info.operators import Operator nqubits = 4 H = QuantumCircuit(nqubits) for i in range(nqubits): H.z(i) H_op = Operator(H) qaoa = QAOA(optimizer= CobylaOptimizer,reps=1,mixer=H,initial_point=np.array([1.0, 1.0]),quantum_instance=quantum_instance) print(qaoa.compute_minimum_eigenvalue(H_op)) $\endgroup$ Commented May 21, 2022 at 9:46
  • $\begingroup$ Gotcha I see that now, take a look at Steve Wood's answer that seemed to fix it quantumcomputing.stackexchange.com/a/26519/13991 $\endgroup$
    – ryanhill1
    Commented May 21, 2022 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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