Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError when using qiskit_to_tk with certain gates #41

Open
nquetschlich opened this issue Dec 14, 2022 · 4 comments
Open

RuntimeError when using qiskit_to_tk with certain gates #41

nquetschlich opened this issue Dec 14, 2022 · 4 comments
Assignees
Labels
bug Something isn't working circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa enhancement New feature or request

Comments

@nquetschlich
Copy link

nquetschlich commented Dec 14, 2022

I get a RuntimeError when using qiskit_to_tk with certain quantum circuit/gates. One example is the following:

from qiskit.circuit.library import TwoLocal
from pytket.extensions.qiskit import qiskit_to_tk
import numpy as np
qc = TwoLocal(2, "ry", "cx", entanglement="full", reps=3)
qc = qc.bind_parameters(np.random.rand(qc.num_parameters))
qc_tket = qiskit_to_tk(qc)

leading to the error

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/folders/<...>.py in <module>
      5 qc = qc.bind_parameters(np.random.rand(qc.num_parameters))
      6 
----> 7 qc_tket = qiskit_to_tk(qc)

~/<...>/site-packages/pytket/extensions/qiskit/qiskit_convert.py in qiskit_to_tk(qcirc, preserve_param_uuid)
    374         phase=param_to_tk(qcirc.global_phase),
    375     )
--> 376     builder.add_qiskit_data(qcirc.data)
    377     return builder.circuit()
    378 

~/<...>/site-packages/pytket/extensions/qiskit/qiskit_convert.py in add_qiskit_data(self, data)
    330                         i.name, subc, list(subc.free_symbols())
    331                     )
--> 332                     self.tkc.add_custom_gate(gate_def, params, qubits + bits)
    333             elif optype == OpType.CU3 and type(i) == qiskit_gates.CUGate:
    334                 if i.params[-1] == 0:

RuntimeError: Gate has an invalid number of parameters

Here, the Qiskit QuantumCircuit contains just one TwoLocal gate.
However, when I decompose the quantum circuit first, the transformation works:

qc_tket = qiskit_to_tk(qc.decompose())

Similar errors occur when trying to transform other circuits with other non-openqasm gates.

I used python v3.9.7, pytket_qiskit v0.32.0, pytket v1.9.0, and qiskit v0.39.3 under Mac OS 13.0.1.

@CalMacCQ
Copy link
Contributor

CalMacCQ commented Dec 14, 2022

Thanks for reporting the issue.

This seems to come up often when there is some higher level operation in qiskit which needs to be decomposed before the conversion will work. The error message could certainly be more useful as well.

I think I will make a PR soon to call QuantumCircuit.decompose() automatically in the qiskit_to_tk conversion. Its probably a bit more user friendly if the conversion to tket handles these cases.

@nquetschlich
Copy link
Author

Thanks for reporting the issue.

This seems to come up often when there is some higher level operation in qiskit which needs to be decomposed before the conversion will work. The error message could certainly be more useful as well.

I think I will make a PR soon to call QuantumCircuit.decompose() automatically in the qiskit_to_tk conversion. Its probably a bit more user friendly if the conversion to tket handles these cases.

Thank you very much for your fast response.

Actually I think one decompose() call is not enough, since that only decomposes one level and the error could happen also on the subsequent one. Maybe a check should be added if there are gates which cannot be handled and only those are handed over to the gates_to_decompose parameter, e.g., like decompose(gates_to_decompose=<...>).

@CalMacCQ CalMacCQ added bug Something isn't working enhancement New feature or request labels Dec 14, 2022
@CalMacCQ
Copy link
Contributor

Yes this sounds like a good idea.

I think it should be possible to do this check that you suggest and target the unsupported operations specifically.

@CalMacCQ
Copy link
Contributor

I should've said... you're welcome to submit a PR for this change if you have the time.

Otherwise I'll probably make a fix sometime in the next week or two :)

@CalMacCQ CalMacCQ added the circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working circuit_conversion Issues and pull requests related to coverting qiskit circuits to pytket and vice versa enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants