CQCL/pytket-qiskit

Error in Parameter Handling

Closed this issue · 7 comments

Hi there,

when experimenting with compiling a parameterized quantum circuit partly with Qiskit and partly with TKET using the following example:

from pytket.extensions.qiskit import qiskit_to_tk, tk_to_qiskit 
from pytket.passes import CliffordSimp
from qiskit.circuit.library import TwoLocal
from qiskit import transpile

qc = TwoLocal(1, "ry", "cz", reps=1, entanglement="linear")
qc_transpiled = transpile(qc, basis_gates=["sx", "rz", "cx", "x"], optimization_level=3)

tket_qc = qiskit_to_tk(qc_transpiled)
CliffordSimp().apply(tket_qc)
transformed_qc = tk_to_qiskit(tket_qc)

qc_transpiled_again = transpile(transformed_qc, basis_gates=["sx", "rz", "cx", "x"])

qc_transpiled_again = qc_transpiled_again.assign_parameters({qc_transpiled_again.parameters[i]: 0 for i in range(len(qc_transpiled_again.parameters))})

I face a TypeError: could not cast expression to complex error.
Apparently, there is some error in the parameter handling in combination with sympy. When executing

expr = qc_transpiled_again.data._circuit._data[2].operation.definition.data._circuit.global_phase
assigned_expr = expr.assign(qc_transpiled_again.parameters[0],1).assign(qc_transpiled_again.parameters[1],1)
print(assigned_expr)
print(assigned_expr._symbol_expr)
print(assigned_expr.is_real())

the expression is apparently not real, although the symbolic representation can be reduced to a real number. Even a assigned_expr._symbol_expr.evalf() does not reduce the assigned_expr and returns -0.5*(3.14159265358979 - 2*atan2(-0.841470984807895, 0.540302305868143)). Maybe the atan2 causes the problem?

I used Qiskit v0.45.3, pytket_qiskit v0.48.0 using python 3.11 under Mac OS v14.2.1.

Hi there,

unfortunately, the error still persists in pytket-qiskit v0.50.0 with qiskit v1.0.2. However, the error message is now a bit different: TypeError: EvalfMixin.evalf() got an unexpected keyword argument 'real'

It would be highly appreciated if you could have a look into this issue, since it currently prevents a smooth combination of compilation passes from both qiskit and tket within our MQT Predictor framework.

Interestingly, when I try to convert the quantum circuit after the tket CliffordSimp() via dumping and then loading via the qasm format using

from qiskit import QuantumCircuit
from pytket.qasm import circuit_to_qasm_str

transformed_qc = QuantumCircuit.from_qasm_str(circuit_to_qasm_str(tket_qc))

I get a different error: QASM2ParseError: "'atan2' is not a parameter or custom instruction defined in this scope"

Thank you for reporting this, I will take a look and try to understand what is going on

Thank you for reporting this, I will take a look and try to understand what is going on

Thanks a lot for that. I was wondering whether you already had the time to have a look?

Hi @nquetschlich
Thank you for being so patient, the issues was a bit tricky.
I have been able to reduce this to a problem in qiskit interacting with sympy and I have submitted a bug report.
I will keep an eye on this and try to update as soon as this is fixed.
More details can be found at: Qiskit/qiskit#12144

The issue in qiskit was caused by an updated and what we were trying in the conversion is not expected to work any more.

I have updated that now and put out a release candidate.

Can you please check if this solved all the issues you have?

You can install it with pip install pytket-qiskit==0.51.1rc0

The issue in qiskit was caused by an updated and what we were trying in the conversion is not expected to work any more.

I have updated that now and put out a release candidate.

Can you please check if this solved all the issues you have?

You can install it with pip install pytket-qiskit==0.51.1rc0

Thanks a lot for fixing this issue. I tried it out and it works.