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

Support save_statevector for QuantumCircuit from QASM3 string #1846

Merged
merged 6 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions qiskit_aer/library/default_qubits.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ def default_qubits(circuit, qubits=None):
for tuple_element in tuples:
if isinstance(tuple_element, QuantumRegister):
for j in range(tuple_element.size):
qubits.append(tuple_element[j])
if tuple_element[j] not in qubits:
qubits.append(tuple_element[j])
else:
qubits.append(tuple_element)
if tuple_element not in qubits:
qubits.append(tuple_element)

return qubits
Original file line number Diff line number Diff line change
@@ -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()``
retrieves qubits from quantum registers without duplication.
38 changes: 38 additions & 0 deletions test/terra/backends/aer_simulator/test_save_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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
Expand Down Expand Up @@ -201,3 +202,40 @@ 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"""
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)