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 qpy for controlled UnitaryGate #10809

Merged
merged 10 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ def _loads_instruction_parameter(
elif type_key == type_keys.Value.REGISTER:
param = _loads_register_param(data_bytes.decode(common.ENCODE), circuit, registers)
else:
clbits = circuit.clbits if circuit is not None else ()
param = value.loads_value(
type_key,
data_bytes,
version,
vectors,
clbits=circuit.clbits,
clbits=clbits,
cregs=registers["c"],
use_symengine=use_symengine,
)
Expand Down Expand Up @@ -577,12 +578,21 @@ def _write_instruction(file_obj, instruction, custom_operations, index_map, use_
or gate_class_name == "Instruction"
or isinstance(instruction.operation, library.BlueprintCircuit)
):
if instruction.operation.name not in custom_operations:
custom_operations[instruction.operation.name] = instruction.operation
custom_operations_list.append(instruction.operation.name)
gate_class_name = instruction.operation.name
# ucr*_dg gates can have different numbers of parameters,
# the uuid is appended to avoid storing a single definition
# in circuits with multiple ucr*_dg gates.
if instruction.operation.name in ["ucrx_dg", "ucry_dg", "ucrz_dg"]:
gate_class_name += "_" + str(uuid.uuid4())

if gate_class_name not in custom_operations:
custom_operations[gate_class_name] = instruction.operation
custom_operations_list.append(gate_class_name)

elif gate_class_name == "ControlledGate":
# controlled gates can have the same name but different parameter
# values, the uuid is appended to avoid storing a single definition
# in circuits with multiple controlled gates.
gate_class_name = instruction.operation.name + "_" + str(uuid.uuid4())
custom_operations[gate_class_name] = instruction.operation
custom_operations_list.append(gate_class_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixed a bug in QPY serialization (:mod:`qiskit.qpy`) where controlled unitary gates in
a circuit could result would fail to deserialize. Fixed `#10802
<https://github.com/Qiskit/qiskit-terra/issues/10802>`__.
16 changes: 16 additions & 0 deletions test/python/circuit/test_circuit_load_from_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,22 @@ def test_unitary_gate(self):
self.assertEqual(qc, new_circ)
self.assertDeprecatedBitProperties(qc, new_circ)

def test_controlled_unitary_gate(self):
"""Test that numpy array parameters are correctly serialized
in controlled unitary gate."""
qc = QuantumCircuit(2)
unitary = np.array([[0, 1], [1, 0]])
gate = UnitaryGate(unitary)
qc.append(gate.control(1), [0, 1])

with io.BytesIO() as qpy_file:
dump(qc, qpy_file)
qpy_file.seek(0)
new_circ = load(qpy_file)[0]

self.assertEqual(qc.decompose(reps=5), new_circ.decompose(reps=5))
self.assertDeprecatedBitProperties(qc, new_circ)

def test_opaque_gate(self):
"""Test that custom opaque gate is correctly serialized"""
custom_gate = Gate("black_box", 1, [])
Expand Down