SimoneGasperini / qiskit-symb

Python package for the symbolic evaluation of parameterized quantum circuits in Qiskit
https://pypi.org/project/qiskit-symb/
Apache License 2.0
26 stars 2 forks source link

Symbolic evaluation of `CUGate` raises an unexpected `TypeError` #4

Closed adrianor closed 10 months ago

adrianor commented 1 year ago

There is an issue when one tries to use the package and the circuit contains controlled unitaries. For instance the circuit

from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit, Parameter
import numpy as np

st0 = Parameter("st0")
st1 = Parameter("st1")

def u1red_cr_overrot(alpha, theta):
    qreg_q = QuantumRegister(2, "q")
    creg_c = ClassicalRegister(2, "c")
    circuit = QuantumCircuit(qreg_q, creg_c)
    circuit.u(1.570796326794897, -2.9755113931440857, 4.71238898038469 + st1, qreg_q[1])
    circuit.u(1.8942714348583476, -4.71238898038469, 0.0 + st0, qreg_q[0])
    circuit.cu(np.pi / 2, np.pi / 2, -np.pi / 2, 0, qreg_q[1], qreg_q[0])
    return qreg_q, creg_c, circuit

raises no problem when one draws it:

phin = 0.0
theta = np.pi / 8.0

qreg_q, creg_c, circuit = u1red_cr_overrot(phin, theta)
circuit.draw("mpl")

But if one tries to get its Statevector form

from qiskit_symb.quantum_info import Statevector

statevec = Statevector(circuit)

it raises the following error message:

TypeError: CUGate.__init__() got multiple values for argument 'ctrl_qubits'

Note that this also happens if one wants to obtain its Operator form:

from qiskit_symb.quantum_info import Operator

op = Operator(circuit)
SimoneGasperini commented 1 year ago

Thank you @adrianor for reporting this. The problem here arises from a minor difference in the signature of the controlled unitary gate in Qiskit and in qiskit-symb. In Qiskit, the CUGate constructor (and the corresponding QuantumCircuit.cu method you are using in your code) takes 3 angles theta, phi, lam + the global phase gamma while, in qiskit-symb, the last parameter gamma is omitted. From my point of view, this is actually a small but relevant inconsistency in Qiskit, since CUGate is the only gate that takes one extra "angle parameter" compared to its corresponding UGate (not controlled) and indeed several open issues on the main qiskit-terra repository are related to this problem (e.g. https://github.com/Qiskit/qiskit-terra/issues/9763, https://github.com/Qiskit/qiskit-terra/issues/7326).

However, this is for sure not handled correctly in qiskit-symb and the error message is very misleading. So, waiting for the problem to be properly solved in Qiskit itself, I can temporarily fix qiskit-symb so that it shows an appropriate error message by marking the CUGate as "not implemented".

SimoneGasperini commented 1 year ago

Meanwhile, you can use the Qiskit transpiler to work around the problem and run your code with no errors:

import numpy as np
from qiskit import transpile
from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit, Parameter
from qiskit_symb.utils import get_symbolic_gates_names

st0 = Parameter("st0")
st1 = Parameter("st1")

def u1red_cr_overrot(alpha, theta):
    qreg_q = QuantumRegister(2, "q")
    creg_c = ClassicalRegister(2, "c")
    circuit = QuantumCircuit(qreg_q, creg_c)
    circuit.u(1.570796326794897, -2.9755113931440857, 4.71238898038469 + st1, qreg_q[1])
    circuit.u(1.8942714348583476, -4.71238898038469, 0.0 + st0, qreg_q[0])
    circuit.cu(np.pi / 2, np.pi / 2, -np.pi / 2, 0, qreg_q[1], qreg_q[0])
    symbolic_gates = get_symbolic_gates_names()
    symbolic_gates.remove("cu")
    circuit = transpile(circuit, basis_gates=symbolic_gates)
    return qreg_q, creg_c, circuit

In this case, the CUGate transforms into a CU3Gate but the final circuit is always exactly equivalent to the original. Note that this also works correctly when the global phase parameter gamma is not 0.