diff --git a/qiskit_aer/library/default_qubits.py b/qiskit_aer/library/default_qubits.py index df251d158d..bc446e73f9 100644 --- a/qiskit_aer/library/default_qubits.py +++ b/qiskit_aer/library/default_qubits.py @@ -13,7 +13,7 @@ Helper function """ -from qiskit.circuit import QuantumCircuit, QuantumRegister +from qiskit.circuit import QuantumRegister from qiskit.extensions.exceptions import ExtensionError @@ -38,18 +38,8 @@ def default_qubits(circuit, qubits=None): if isinstance(qubits, QuantumRegister): qubits = qubits[:] if not qubits: - tuples = [] - if isinstance(circuit, QuantumCircuit): - for register in circuit.qregs: - tuples.append(register) - if not tuples: + qubits = list(circuit.qubits) + if len(qubits) == 0: raise ExtensionError("no qubits for snapshot") - qubits = [] - for tuple_element in tuples: - if isinstance(tuple_element, QuantumRegister): - for j in range(tuple_element.size): - qubits.append(tuple_element[j]) - else: - qubits.append(tuple_element) return qubits diff --git a/releasenotes/notes/save_statevector_for_qasm3_circ-642ade99af3ff0d2.yaml b/releasenotes/notes/save_statevector_for_qasm3_circ-642ade99af3ff0d2.yaml new file mode 100644 index 0000000000..2db81d60ff --- /dev/null +++ b/releasenotes/notes/save_statevector_for_qasm3_circ-642ade99af3ff0d2.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + :meth:``QuantumCircuit.save_statevector()`` does not work if the circuit + is generated from OpenQASM3 text because its quantum registers have duplicated + qubit instances. With this commit, :meth:``QuantumCircuit.save_statevector()`` + uses :data:``QuantumCircuit.qubits`` to get qubits to be saved. diff --git a/requirements-dev.txt b/requirements-dev.txt index 7e21763be6..d1a45eda8b 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -16,3 +16,4 @@ seaborn>=0.9.0 qiskit_sphinx_theme~=1.12.0 sphinx-design>=0.2.0 nbsphinx +qiskit_qasm3_import diff --git a/test/terra/backends/aer_simulator/test_save_statevector.py b/test/terra/backends/aer_simulator/test_save_statevector.py index c8f374a54d..d5353dc49c 100644 --- a/test/terra/backends/aer_simulator/test_save_statevector.py +++ b/test/terra/backends/aer_simulator/test_save_statevector.py @@ -13,10 +13,12 @@ Integration Tests for SaveStatevector instruction """ +import sys from ddt import ddt import qiskit.quantum_info as qi from qiskit import QuantumCircuit, transpile from test.terra.backends.simulator_test_case import SimulatorTestCase, supported_methods +from qiskit.qasm3 import dumps, loads @ddt @@ -201,3 +203,44 @@ def test_save_statevector_cache_blocking(self, method, device): self.assertIn(label, simdata) value = simdata[label] self.assertEqual(value, target) + + @supported_methods( + [ + "automatic", + "statevector", + "matrix_product_state", + "extended_stabilizer", + "tensor_network", + ] + ) + def test_save_statevector_for_qasm3_circuit(self, method, device): + """Test save statevector instruction""" + # qiskit_qasm3_import, which is used in qiskit.qasm3 does not support 3.7 + if sys.version_info < (3, 8): + return + + backend = self.backend(method=method, device=device) + + # Stabilizer test circuit + circ = QuantumCircuit(3) + circ.h(0) + circ.sdg(0) + circ.cx(0, 1) + circ.cx(0, 2) + + # Target statevector + target = qi.Statevector(circ) + + circ = loads(dumps(circ)) + + # Add save to circuit + label = "state" + circ.save_statevector(label=label) + + # Run + result = backend.run(transpile(circ, backend, optimization_level=0), shots=1).result() + self.assertTrue(result.success) + simdata = result.data(0) + self.assertIn(label, simdata) + value = simdata[label] + self.assertEqual(value, target)