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

Fix RuntimeError when converting qiskit instructions. #203

Merged
merged 12 commits into from
Nov 15, 2023

Conversation

CalMacCQ
Copy link
Contributor

@CalMacCQ CalMacCQ commented Nov 14, 2023

Addresses #200

Consider the following example

import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import RealAmplitudes
from pytket.extensions.qiskit import qiskit_to_tk

qc = QuantumCircuit(3)

params = [np.pi/2]*9
supply1 = RealAmplitudes(3, reps=2)
   
supply1 = supply1.assign_parameters(params)
qc.compose(supply1, qubits = [0, 1, 2], inplace=True)

If we try to convert qc to a pytket Circuit with qiskit_to_tk then we get

RuntimeError: Gate has an invalid number of parameters

Now pytket-qiskit converts the instruction to a CustomGate. The actual gate definition works fine but the Circuit.add_custom_gate() method is failing because there are no free symbols in the circuit.

I've decided to just handle this case with CircBox as I think its simpler.

There is still one assert failure in test which I'm puzzled by. This is the one that checks the name of the circuit.
EDIT: Resolved

@CalMacCQ CalMacCQ requested a review from cqc-melf as a code owner November 14, 2023 17:33
@CalMacCQ CalMacCQ requested a review from cqc-alec November 15, 2023 10:25
@CalMacCQ CalMacCQ changed the title Fix/custom gate conversion Fix RuntimeError when converting qiskit instructions. Nov 15, 2023
@@ -24,9 +24,9 @@
ClassicalRegister,
execute,
)
from qiskit.quantum_info import Pauli, SparsePauliOp # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that pylint didn't pick up this unused import in previous workflow runs.

I'd expect it to complain about this.

qc2 = tk_to_qiskit(converted_tkc)
tkc2 = qiskit_to_tk(qc2)
unitary2 = tkc2.get_unitary()
assert compare_unitaries(unitary1, unitary2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness can we also check the unitary against that of the original qiskit circuit?

from qiskit.quantum_info import Operator

unitary0 = Operator(qc).data

(Might have to correct for endianness?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense. Done.

@CalMacCQ CalMacCQ merged commit fc18d49 into develop Nov 15, 2023
7 checks passed
@CalMacCQ CalMacCQ deleted the fix/Custom_gate_conversion branch February 23, 2024 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants