Skip to content

Commit

Permalink
Add tests and release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Mar 25, 2021
1 parent df31e7b commit b1c152c
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
19 changes: 19 additions & 0 deletions releasenotes/notes/set-state-a029d86b357df0dd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
features:
- |
Adds instructions for setting the state of the simulators. These
instructions must be defined on the full number of qubits in the circuit.
They can be applied at any point in a circuit and will override the
simulator state with the one specified. Added instructions are
* :class:`qiskit.providers.aer.library.SetStatevector`
* :class:`qiskit.providers.aer.library.SetDensityMatrix`
* :class:`qiskit.providers.aer.library.SetStabilizer`
* :class:`qiskit.providers.aer.library.SetUnitary`
These instruction can also be appended to a quantum circuit by using the
:class:`~qiskit.providers.aer.library.set_statevector`,
:class:`~qiskit.providers.aer.library.set_density_matrix`,
:class:`~qiskit.providers.aer.library.set_stabilizer`,
:class:`~qiskit.providers.aer.library.set_unitary`
circuit methods which are added to ``QuantumCircuit`` when importing Aer.
122 changes: 122 additions & 0 deletions test/terra/backends/qasm_simulator/qasm_set_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
QasmSimulator Integration Tests for set state instructions
"""

from ddt import ddt, data
import qiskit.quantum_info as qi
from qiskit import QuantumCircuit, assemble
from qiskit.providers.aer import QasmSimulator


@ddt
class QasmSetStateTests:
"""QasmSimulator set state instruction tests."""

SIMULATOR = QasmSimulator()
BACKEND_OPTS = {}

@data(1, 2, 3, 4, 5)
def test_set_statevector(self, num_qubits):
"""Test SetStatevector for instruction"""

SUPPORTED_METHODS = [
'automatic', 'statevector', 'statevector_gpu',
'statevector_thrust'
]

seed = 100
save_label = 'state'

target = qi.random_statevector(2 ** num_qubits, seed=seed)

circ = QuantumCircuit(num_qubits)
circ.set_statevector(target)
circ.save_statevector(label=save_label)

# Run
opts = self.BACKEND_OPTS.copy()
qobj = assemble(circ, self.SIMULATOR)
result = self.SIMULATOR.run(qobj, **opts).result()
method = opts.get('method', 'automatic')
if method not in SUPPORTED_METHODS:
self.assertFalse(result.success)
else:
self.assertTrue(result.success)
data = result.data(0)
self.assertIn(save_label, data)
value = qi.Statevector(result.data(0)[save_label])
self.assertAlmostEqual(value, target)

@data(1, 2, 3)
def test_set_density_matrix(self, num_qubits):
"""Test SetDensityMatrix instruction"""

SUPPORTED_METHODS = [
'automatic', 'density_matrix', 'density_matrix_gpu',
'density_matrix_thrust'
]

seed = 100
save_label = 'state'

target = qi.random_density_matrix(2 ** num_qubits, seed=seed)

circ = QuantumCircuit(num_qubits)
circ.set_density_matrix(target)
circ.save_density_matrix(label=save_label)

# Run
opts = self.BACKEND_OPTS.copy()
qobj = assemble(circ, self.SIMULATOR)
result = self.SIMULATOR.run(qobj, **opts).result()
method = opts.get('method', 'automatic')
if method not in SUPPORTED_METHODS:
self.assertFalse(result.success)
else:
self.assertTrue(result.success)
data = result.data(0)
self.assertIn(save_label, data)
value = qi.DensityMatrix(result.data(0)[save_label])
self.assertAlmostEqual(value, target)

@data(1, 2, 3)
def test_set_stabilizer(self, num_qubits):
"""Test SetStabilizer instruction"""

SUPPORTED_METHODS = [
'automatic', 'stabilizer'
]

seed = 100
save_label = 'state'

target = qi.random_clifford(num_qubits, seed=seed)

circ = QuantumCircuit(num_qubits)
circ.set_stabilizer(target)
circ.save_stabilizer(label=save_label)

# Run
opts = self.BACKEND_OPTS.copy()
qobj = assemble(circ, self.SIMULATOR)
result = self.SIMULATOR.run(qobj, **opts).result()
method = opts.get('method', 'automatic')
if method not in SUPPORTED_METHODS:
self.assertFalse(result.success)
else:
self.assertTrue(result.success)
data = result.data(0)
self.assertIn(save_label, data)
value = qi.Clifford.from_dict(result.data(0)[save_label])
self.assertEqual(value, target)
2 changes: 2 additions & 0 deletions test/terra/backends/test_qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from test.terra.backends.qasm_simulator.qasm_noise import QasmKrausNoiseTests
# Save data tests
from test.terra.backends.qasm_simulator.qasm_save import QasmSaveDataTests
from test.terra.backends.qasm_simulator.qasm_set_state import QasmSetStateTests
# Snapshot tests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotStatevectorTests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotDensityMatrixTests
Expand Down Expand Up @@ -86,6 +87,7 @@ class TestQasmSimulator(common.QiskitAerTestCase,
QasmKrausNoiseTests,
QasmBasicsTests,
QasmSaveDataTests,
QasmSetStateTests,
QasmStandardGateStatevectorTests,
QasmStandardGateDensityMatrixTests,
QasmDelayGateTests,
Expand Down
3 changes: 2 additions & 1 deletion test/terra/backends/test_qasm_simulator_density_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from test.terra.backends.qasm_simulator.qasm_fusion import QasmFusionTests
# Save data tests
from test.terra.backends.qasm_simulator.qasm_save import QasmSaveDataTests
from test.terra.backends.qasm_simulator.qasm_set_state import QasmSetStateTests
# Snapshot tests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotStatevectorTests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotDensityMatrixTests
Expand All @@ -63,7 +64,7 @@ class DensityMatrixTests(
QasmAlgorithmTests, QasmAlgorithmTestsWaltzBasis,
QasmAlgorithmTestsMinimalBasis, QasmUnitaryGateTests, QasmDiagonalGateTests,
QasmReadoutNoiseTests, QasmPauliNoiseTests, QasmResetNoiseTests,
QasmKrausNoiseTests, QasmSaveDataTests,
QasmKrausNoiseTests, QasmSaveDataTests, QasmSetStateTests,
QasmSnapshotStatevectorTests,
QasmSnapshotDensityMatrixTests, QasmSnapshotProbabilitiesTests,
QasmSnapshotExpValPauliTests, QasmSnapshotExpValPauliNCTests,
Expand Down
2 changes: 2 additions & 0 deletions test/terra/backends/test_qasm_simulator_stabilizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from test.terra.backends.qasm_simulator.qasm_noise import QasmResetNoiseTests
# Save data tests
from test.terra.backends.qasm_simulator.qasm_save import QasmSaveDataTests
from test.terra.backends.qasm_simulator.qasm_set_state import QasmSetStateTests
# Snapshot tests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotStatevectorTests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotDensityMatrixTests
Expand All @@ -56,6 +57,7 @@ class TestQasmStabilizerSimulator(common.QiskitAerTestCase,
QasmResetNoiseTests,
QasmPauliNoiseTests,
QasmSaveDataTests,
QasmSetStateTests,
QasmSnapshotStatevectorTests,
QasmSnapshotDensityMatrixTests,
QasmSnapshotProbabilitiesTests,
Expand Down
3 changes: 2 additions & 1 deletion test/terra/backends/test_qasm_simulator_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from test.terra.backends.qasm_simulator.qasm_noise import QasmKrausNoiseTests
# Save data tests
from test.terra.backends.qasm_simulator.qasm_save import QasmSaveDataTests
from test.terra.backends.qasm_simulator.qasm_set_state import QasmSetStateTests
# Snapshot tests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotStatevectorTests
from test.terra.backends.qasm_simulator.qasm_snapshot import QasmSnapshotDensityMatrixTests
Expand All @@ -71,7 +72,7 @@ class StatevectorTests(
QasmReadoutNoiseTests, QasmPauliNoiseTests, QasmThreadManagementTests,
QasmFusionTests, QasmDelayMeasureTests, QasmQubitsTruncateTests,
QasmResetNoiseTests, QasmKrausNoiseTests, QasmBasicsTests,
QasmSaveDataTests,
QasmSaveDataTests, QasmSetStateTests,
QasmSnapshotStatevectorTests, QasmSnapshotDensityMatrixTests,
QasmSnapshotProbabilitiesTests, QasmSnapshotExpValPauliTests,
QasmSnapshotExpValPauliNCTests, QasmSnapshotExpValMatrixTests,
Expand Down

0 comments on commit b1c152c

Please sign in to comment.