Skip to content

Commit

Permalink
Fix qpy import of circuit with StatePreparation (#9665) (#9711)
Browse files Browse the repository at this point in the history
* add StatePreparation to list

* add reno

* add test

* fix lint

* Reword release note

---------

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit dc1737c)

Co-authored-by: Kazuki Tsuoka <[email protected]>
  • Loading branch information
mergify[bot] and king-p3nguin authored Mar 2, 2023
1 parent 40cafa4 commit ac9f9b9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qiskit/qpy/binary_io/circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _read_instruction(file_obj, circuit, registers, custom_operations, version,
gate.ctrl_state = instruction.ctrl_state
gate.condition = condition_tuple
else:
if gate_name in {"Initialize", "UCRXGate", "UCRYGate", "UCRZGate"}:
if gate_name in {"Initialize", "StatePreparation", "UCRXGate", "UCRYGate", "UCRZGate"}:
gate = gate_class(params)
else:
if gate_name == "Barrier":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
QPY (using :func:`.qpy.load`) will now correctly deserialize :class:`~.StatePreparation`
instructions. Previously, QPY would error when attempting to load a file containing one.
Fixed `#8297 <https://github.com/Qiskit/qiskit-terra/issues/8297>`__.
31 changes: 31 additions & 0 deletions test/python/circuit/test_circuit_load_from_qpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,37 @@ def test_initialize_qft(self):
)
self.assertDeprecatedBitProperties(qc, new_circ)

def test_statepreparation(self):
"""Test that state preparation with a complex statevector and qft work."""
k = 5
state = (1 / np.sqrt(8)) * np.array(
[
np.exp(-1j * 2 * np.pi * k * (0) / 8),
np.exp(-1j * 2 * np.pi * k * (1) / 8),
np.exp(-1j * 2 * np.pi * k * (2) / 8),
np.exp(-1j * 2 * np.pi * k * 3 / 8),
np.exp(-1j * 2 * np.pi * k * 4 / 8),
np.exp(-1j * 2 * np.pi * k * 5 / 8),
np.exp(-1j * 2 * np.pi * k * 6 / 8),
np.exp(-1j * 2 * np.pi * k * 7 / 8),
]
)

qubits = 3
qc = QuantumCircuit(qubits, qubits)
qc.prepare_state(state)
qc.append(QFT(qubits), range(qubits))
qc.measure(range(qubits), range(qubits))
qpy_file = io.BytesIO()
dump(qc, qpy_file)
qpy_file.seek(0)
new_circ = load(qpy_file)[0]
self.assertEqual(qc, new_circ)
self.assertEqual(
[x.operation.label for x in qc.data], [x.operation.label for x in new_circ.data]
)
self.assertDeprecatedBitProperties(qc, new_circ)

def test_single_bit_teleportation(self):
"""Test a teleportation circuit with single bit conditions."""
qr = QuantumRegister(1)
Expand Down

0 comments on commit ac9f9b9

Please sign in to comment.