2
$\begingroup$

Qiskit provides the qiskit.quantum_info.Operator class to get the unitary matrix operator from the corresponding quantum circuit, as in the following example:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.visualization import array_to_latex

qc = QuantumCircuit(2)
qc.h(0)

op = Operator(qc)
array_to_latex(op)

\begin{bmatrix} \frac{1}{\sqrt2} & \frac{1}{\sqrt2} & 0 & 0\\ \frac{1}{\sqrt2} & -\frac{1}{\sqrt2} & 0 & 0\\ 0 & 0 & \frac{1}{\sqrt2} & \frac{1}{\sqrt2} \\ 0 & 0 & \frac{1}{\sqrt2} & -\frac{1}{\sqrt2} \end{bmatrix}

However, Operator(QuantumCircuit) raises an error in the case of a parametric quantum circuit:

from qiskit.circuit import Parameter

qc = QuantumCircuit(2)
theta = Parameter(name='$\\theta$')
qc.ry(theta, 0)

op = Operator(qc)  # ERROR!

This brings me to the question: is there a way in Qiskit to get the matrix operator symbolic representation from a given arbitrary PQC? For instance, in this case I would like to get a sympy.matrices.dense.Matrix object (with just one parameter $\theta$) like this:

\begin{bmatrix} \cos\left(\frac{\theta}{2}\right) & -\sin\left(\frac{\theta}{2}\right) & 0 & 0\\ \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) & 0 & 0\\ 0 & 0 & \cos\left(\frac{\theta}{2}\right) & -\sin\left(\frac{\theta}{2}\right)\\ 0 & 0 & \sin\left(\frac{\theta}{2}\right) & \cos\left(\frac{\theta}{2}\right) \end{bmatrix}

EDIT: this is now possible by using the new qiskit-symb package

$\endgroup$
2
  • $\begingroup$ I don't think Qiskit uses sympy at all. $\endgroup$ Commented Dec 15, 2022 at 20:28
  • $\begingroup$ I think it does... At least sympy is included in the requirements.txt file of the qiskit-terra repository $\endgroup$ Commented Dec 15, 2022 at 20:31

3 Answers 3

2
$\begingroup$

I do not believe this is currently possible. Here is a relevant open issue on the Qiskit repository. https://github.com/Qiskit/qiskit-terra/issues/4751

$\endgroup$
0
1
$\begingroup$

As a workaround you could convert your qiskit QuantumCircuit to a pytket Circuit using qiskit_to_tk and then use circuit_to_symbolic_unitary.

This does however involve installing the pytket-qiskit package so I'm not sure this really gives you what you're looking for tbh.

EDIT: pytket and qiskit use different qubit ordering conventions (see comments). Use QuantumCircuit.reverse_bits() before conversion to swap the ordering of the qubits. This reordering is necessary for the unitary to match the one in the question.

from qiskit import QuantumCircuit
from qiskit.circuit import Parameter

# define qiskit circuit as before
qc = QuantumCircuit(2)
theta = Parameter(name='\\theta')
qc.ry(theta, 0) 

Now convert circuit and display unitary

from pytket.extensions.qiskit import qiskit_to_tk
from pytket.utils.symbolic import circuit_to_symbolic_unitary

# optional to change qubit ordering -- qc = qc.reverse_bits() 
tkc = qiskit_to_tk(qc)
circuit_to_symbolic_unitary(tkc)

enter image description here

$\endgroup$
4
  • $\begingroup$ Yes, I was looking for a way to do it in Qiskit but this is not possible at the moment so thank you anyway for the help. However, taking a look at your example, it seems that Qiskit and pytket are using a different qubits ordering convention and so the symbolic unitary matrix I would expect from Qiskit ($I \otimes R_y(\theta)$) is different from the one returned by pytket ($R_y(\theta) \otimes I$). $\endgroup$ Commented Jan 11, 2023 at 11:33
  • 1
    $\begingroup$ Yes, good point. Qiskit and pytket have opposite qubit ordering conventions. I should have mentioned that. I'm a contributor to tket/pytket and I actually think it'd be nice to add an optional argument to reverse order the qubits when converting from qiskit. Would avoid some confusion I think. It already exists for the tk_to_qiskit function and should be fairly easy to add for the other direction. Thanks $\endgroup$
    – Callum
    Commented Jan 11, 2023 at 20:33
  • $\begingroup$ In fact you can use the QuantumCircuit.reverse_bits() method before conversion to get the ordering correct. $\endgroup$
    – Callum
    Commented Jan 13, 2023 at 16:03
  • $\begingroup$ Of course, thank you again. Just make sure that qc == tk_to_qiskit(qiskit_to_tk(qc)) $\endgroup$ Commented Jan 13, 2023 at 16:54
0
$\begingroup$

You can apply an operator to a state, and see the symbolic result. Unfortunately, I can't find any way of getting this to work for an operator.

from sympy.physics.quantum.gate import CNOT, H
from sympy.physics.quantum.qapply import qapply
from sympy.physics.quantum.qubit import Qubit


qapply(CNOT(0, 1) * H(0) * Qubit('000'))

which will return $$\frac{\sqrt2|000\rangle}{2} + \frac{\sqrt2|011\rangle}{2} $$

It doesn't seem to have rotation operators, though you can write your own. I'd love to find out how to just get the operator matrix.

$\endgroup$

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.