diff --git a/qiskit/qpy/binary_io/circuits.py b/qiskit/qpy/binary_io/circuits.py index 62ef8c885228..e84ed7ef0eb8 100644 --- a/qiskit/qpy/binary_io/circuits.py +++ b/qiskit/qpy/binary_io/circuits.py @@ -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": diff --git a/releasenotes/notes/fix-qpy-import-StatePreparation-e20f8ab07bfe39a3.yaml b/releasenotes/notes/fix-qpy-import-StatePreparation-e20f8ab07bfe39a3.yaml new file mode 100644 index 000000000000..c717f5aa5c8b --- /dev/null +++ b/releasenotes/notes/fix-qpy-import-StatePreparation-e20f8ab07bfe39a3.yaml @@ -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 `__. diff --git a/test/python/circuit/test_circuit_load_from_qpy.py b/test/python/circuit/test_circuit_load_from_qpy.py index ea9823d70873..50f829c6f62d 100644 --- a/test/python/circuit/test_circuit_load_from_qpy.py +++ b/test/python/circuit/test_circuit_load_from_qpy.py @@ -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)