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 6 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
3 changes: 2 additions & 1 deletion qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ def _loads_instruction_parameter(type_key, data_bytes, version, vectors, registe
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, cregs=registers["c"]
type_key, data_bytes, version, vectors, clbits=clbits, cregs=registers["c"]
)

return param
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>`__.
14 changes: 14 additions & 0 deletions test/python/circuit/test_circuit_load_from_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ 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=6), new_circ.decompose(reps=6))
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