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 import of circuit with StatePreparation #9665

Merged
merged 8 commits into from
Mar 2, 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
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