# From QUBO matrix to Ising model in Qiskit

Given a general QUBO matrix $$Q$$ for a quadratic minimization problem, is there a Qiskit way to obtain the Pauli gate list or the Ising model for it? A related question is Qiskit: Taking a QUBO matrix into qubit_op', but it seems to be about a graph related model. The solution given there is also for a particular graph metric for the stable_set.

## 2 Answers

I prepared this code based on Qiskit tutorial. Firstly, lets prepare a QUBO task.

# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
#quadratic optimization
from qiskit.optimization import QuadraticProgram
from qiskit.optimization.converters import QuadraticProgramToQubo

# Prepare QUBO task

#prepare quadratic binary optimization task
task = QuadraticProgram(name = 'QUBO on QC')
task.binary_var(name = 'x')
task.binary_var(name = 'y')
task.binary_var(name = 'z')
task.minimize(linear = [1,-2,3], quadratic = [[0,0.5,-0.5],[0.5,0,1],[-0.5,1,0]])
qubo = QuadraticProgramToQubo().convert(task) #convert to QUBO
print(qubo.export_as_lp_string())


Now, we have a QUBO task and you can convert it to Ising Hamiltonian with this code:

#converting QUBO task to Ising Hamiltonian for simulation on quantum computer
operator, offset = qubo.to_ising()

#operator - unitary operator representing the simulated Hamiltonian
#offset - used after solution on QC to convert objective function value to the proper one

print(operator)



This code converts Ising Hamiltonian to QUBO:

#conversion of Ising Hamiltonian to QUBO
qp = QuadraticProgram()
qp.from_ising(operator, offset, linear = True) #linear=True => x^2 is shown as x since x^2=x for x in {0,1}
print(qp.export_as_lp_string())
`
• Martin, this is greatly helpful. Thanks. Feb 22 at 0:37

Given a matrix $$H$$, you can decompose to $$H$$ into Pauli operators

$$H = \sum_{i} h_{i}H_i = \sum_{i_1,i_2,\cdots, i_n} a_{i_1,i_2,\cdots, i_n}(\sigma_{i_1} \otimes \sigma_{i_2} \otimes \cdots \otimes \sigma_{i_n})$$ where $$\sigma_{i_j}\in {I,X,Y,Z}$$ and $$a_{i_1,i_2,\cdots, i_n} = \dfrac{1}{2^n} Tr( (\sigma_{i_1} \otimes \sigma_{i_2} \otimes \cdots \otimes \sigma_{i_n}) \cdot H) = \dfrac{1}{2^n} Tr( H_i \cdot H)$$

I don't know whether qiskit has a converter that takes an arbitrary matrix to this Pauli strings decomposition or not. However, Pennylane does and it is pretty to use. You can import in pennylane to do this task and extract the output to do what you need in qiskit. Or maybe you will just end up write your own function to do it.

• Hi KAJ226, Pennylane pointer is useful. However, A QUBO matrix is not a Hamiltonian as far as I understand. It needs to be converted to one, and that itself is the process of generating the Ising model or the Pauli gate list. Feb 20 at 18:21