-
Notifications
You must be signed in to change notification settings - Fork 33
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
Serializing/Deserializing a circuit with QPD gates causes a ValueError #417
Comments
To reproduce this error, I ran the following piece of code on the subscircuit "A" that was generated in this tutorial:
|
It looks like serialization causes instructions to be decomposed, as this error is thrown in _define. We don't allow qpd gates to be decomposed if their We had suggested letting the gate decompose randomly according to its distribution, but we preferred to limit randomness in the code. This becomes a problem when you want to run circuit knitting workloads remotely, because you'd like to serialize the |
Thanks. FWIW, here's a full MWE: from pathlib import Path
from tempfile import TemporaryDirectory
from qiskit.circuit.library import EfficientSU2
from qiskit import qpy
from circuit_knitting.cutting import partition_problem
qc = EfficientSU2(4, entanglement="linear", reps=2).decompose()
qc.assign_parameters([0.4] * len(qc.parameters), inplace=True)
partitioned_problem = partition_problem(circuit=qc, partition_labels="AABB")
with TemporaryDirectory() as tmpdir:
with open(Path(tmpdir) / "bell.qpy", "wb") as fd:
qpy.dump(partitioned_problem.subcircuits["A"], fd) The full traceback is
Specifically, the call to -- and definition of --
That, and, crucially: this would be fine for
Agreed. :) |
I don't know if this is a "proper" fix, but the following change to Qiskit would probably save us here. It is worth testing. diff --git a/qiskit/qpy/binary_io/circuits.py b/qiskit/qpy/binary_io/circuits.py
index 2cecd7e31..3c98b8e66 100644
--- a/qiskit/qpy/binary_io/circuits.py
+++ b/qiskit/qpy/binary_io/circuits.py
@@ -740,7 +740,7 @@ def _write_custom_operation(file_obj, name, operation, custom_operations, use_sy
num_ctrl_qubits = operation.num_ctrl_qubits
ctrl_state = operation.ctrl_state
base_gate = operation.base_gate
- elif operation.definition is not None:
+ elif not operation._directive and operation.definition is not None:
has_definition = True
data = common.data_to_binary(operation.definition, write_circuit)
size = len(data) |
The above tweak worked. So does the following, which I feel may be closer to a proper fix: diff --git a/qiskit/circuit/instruction.py b/qiskit/circuit/instruction.py
index 6b5ff71cf..0b2d69ff2 100644
--- a/qiskit/circuit/instruction.py
+++ b/qiskit/circuit/instruction.py
@@ -297,7 +297,7 @@ class Instruction(Operation):
@property
def definition(self):
"""Return definition in terms of other basic gates."""
- if self._definition is None:
+ if self._definition is None and not self._directive:
self._define()
return self._definition I'm running the Qiskit test suite now to see if this change causes any other fallout. |
Another -- perhaps better -- way we could fix this is by making |
…than error Fixes #417 according to #417 (comment)
Oh, interesting. If that works, that's probably better than erroring anyway. We also need to consider that a user can have a QPD circuit with its basis_ids set, serialize it, deserialize it, and now have a circuit which is realized with Qiskit gates. I'm not sure that is intended either. Maybe we should consider this as a whole? |
It almost seems like this might be what is intended by qpy. For instance,
I opened #443 as an issue related to the current one, but yes, you raise another good point too, which is a bit independent of what we decide there (in #443). |
… than raise error (#442) * Make unrealized `SingleQubitQPDGate` has definition of `None` rather than error Fixes #417 according to #417 (comment) * Add smoke test * Add release note * Tweak release note * Mention qpy in release note
ValueError: Missing \'basis_id\': unable to realize SingleQubitQPDGate
is raised when serializing/deserializing a circuit withQPDGate
s.This should not raise an error. The gate should be able to be serialized and deserialized without being decomposed into single-qubit Qiskit gates.
The text was updated successfully, but these errors were encountered: