forked from Qiskit/qiskit-aer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial measure in tensor_network simulation method (Qiskit#299)
* Added support for partial measurement. Still testing * Improved functionality of MPS::apply_measure * Changed sample_measure to also use MPS::apply_measure since it is more efficient * Changed expectation value input to use the string 'Z' rather than the matrix * Added tests for measure in tensor network state
- Loading branch information
1 parent
8d0fe29
commit 50417c8
Showing
8 changed files
with
250 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
test/terra/backends/qasm_simulator/tensor_network_measure.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2018, 2019. | ||
# | ||
# 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. | ||
""" | ||
Tensor Network Integration Tests | ||
This set of tests is copied from qasm_measure.py, but without some | ||
functionality that has not been supported yet (specifically the 'iden' | ||
operation). | ||
""" | ||
|
||
from test.terra.reference import ref_measure | ||
from qiskit.compiler import assemble | ||
from qiskit.providers.aer import QasmSimulator | ||
|
||
class QasmTensorNetworkMeasureTests: | ||
"""QasmSimulator tensor_network measure tests.""" | ||
|
||
SIMULATOR = QasmSimulator() | ||
BACKEND_OPTS = {} | ||
|
||
# --------------------------------------------------------------------- | ||
# Test measure | ||
# --------------------------------------------------------------------- | ||
def test_measure_deterministic_with_sampling(self): | ||
"""Test QasmSimulator measure with deterministic counts with sampling""" | ||
shots = 100 | ||
circuits = ref_measure.measure_circuits_deterministic( | ||
allow_sampling=True) | ||
targets = ref_measure.measure_counts_deterministic(shots) | ||
qobj = assemble(circuits, self.SIMULATOR, shots=shots) | ||
result = self.SIMULATOR.run( | ||
qobj, backend_options=self.BACKEND_OPTS).result() | ||
self.is_completed(result) | ||
self.compare_counts(result, circuits, targets, delta=0) | ||
# Test sampling was enabled | ||
for res in result.results: | ||
self.assertIn("measure_sampling", res.metadata) | ||
self.assertEqual(res.metadata["measure_sampling"], True) | ||
|
||
def test_measure_nondeterministic_with_sampling(self): | ||
"""Test QasmSimulator measure with non-deterministic counts with sampling""" | ||
shots = 2000 | ||
circuits = ref_measure.measure_circuits_nondeterministic( | ||
allow_sampling=True) | ||
targets = ref_measure.measure_counts_nondeterministic(shots) | ||
qobj = assemble(circuits, self.SIMULATOR, shots=shots) | ||
result = self.SIMULATOR.run( | ||
qobj, backend_options=self.BACKEND_OPTS).result() | ||
self.is_completed(result) | ||
self.compare_counts(result, circuits, targets, delta=0.05 * shots) | ||
# Test sampling was enabled | ||
for res in result.results: | ||
self.assertIn("measure_sampling", res.metadata) | ||
self.assertEqual(res.metadata["measure_sampling"], True) | ||
|
||
# --------------------------------------------------------------------- | ||
# Test multi-qubit measure qobj instruction | ||
# --------------------------------------------------------------------- | ||
def test_measure_nondeterministic_multi_qubit_with_sampling(self): | ||
"""Test QasmSimulator measure with non-deterministic counts""" | ||
shots = 2000 | ||
circuits = ref_measure.multiqubit_measure_circuits_nondeterministic( | ||
allow_sampling=True) | ||
targets = ref_measure.multiqubit_measure_counts_nondeterministic(shots) | ||
qobj = assemble(circuits, self.SIMULATOR, shots=shots) | ||
result = self.SIMULATOR.run( | ||
qobj, backend_options=self.BACKEND_OPTS).result() | ||
self.is_completed(result) | ||
self.compare_counts(result, circuits, targets, delta=0.05 * shots) | ||
# Test sampling was enabled | ||
for res in result.results: | ||
self.assertIn("measure_sampling", res.metadata) | ||
self.assertEqual(res.metadata["measure_sampling"], True) | ||
|
71 changes: 71 additions & 0 deletions
71
test/terra/backends/qasm_simulator/tensor_network_method.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2018, 2019. | ||
# | ||
# 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. | ||
""" | ||
Tensor Network Integration Tests | ||
This set of tests runs all the circuits from test/terra/references/: ref_1q_clifford and ref_2q_clifford, | ||
with the exception of those with the multiplexer gate which is not supported yet. | ||
""" | ||
|
||
import unittest | ||
import logging | ||
import pprint | ||
|
||
from test.terra import common | ||
from test.terra.reference import ref_1q_clifford, ref_2q_clifford | ||
|
||
from qiskit import * | ||
from qiskit.providers.aer import QasmSimulator | ||
#from qiskit.extensions.simulator import Snapshot | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class QasmTensorNetworkMethodTests: | ||
"""QasmSimulator tensor_network method tests.""" | ||
|
||
BACKEND_OPTS = {"method": "tensor_network"} | ||
|
||
def test_method_deterministic_without_sampling(self): | ||
"""TestTensorNetwork method with deterministic counts without sampling""" | ||
deterministic_tests = [(ref_1q_clifford.h_gate_circuits_deterministic, ref_1q_clifford.h_gate_counts_deterministic), | ||
(ref_1q_clifford.x_gate_circuits_deterministic, ref_1q_clifford.x_gate_counts_deterministic), | ||
(ref_1q_clifford.z_gate_circuits_deterministic, ref_1q_clifford.z_gate_counts_deterministic), | ||
(ref_1q_clifford.y_gate_circuits_deterministic, ref_1q_clifford.y_gate_counts_deterministic), | ||
(ref_1q_clifford.s_gate_circuits_deterministic, ref_1q_clifford.s_gate_counts_deterministic), | ||
(ref_1q_clifford.sdg_gate_circuits_deterministic, ref_1q_clifford.sdg_gate_counts_deterministic), | ||
(ref_2q_clifford.cx_gate_circuits_deterministic,ref_2q_clifford.cx_gate_counts_deterministic), | ||
(ref_2q_clifford.cz_gate_circuits_deterministic, ref_2q_clifford.cz_gate_counts_deterministic), | ||
(ref_2q_clifford.swap_gate_circuits_deterministic, ref_2q_clifford.swap_gate_counts_deterministic)] | ||
|
||
determ_test_list = {'list':deterministic_tests, 'shots':100, 'delta':0} | ||
|
||
nondeterministic_tests = [(ref_1q_clifford.h_gate_circuits_nondeterministic, ref_1q_clifford.h_gate_counts_nondeterministic), | ||
(ref_1q_clifford.s_gate_circuits_nondeterministic, ref_1q_clifford.s_gate_counts_nondeterministic), | ||
(ref_1q_clifford.sdg_gate_circuits_nondeterministic, ref_1q_clifford.sdg_gate_counts_nondeterministic), | ||
(ref_2q_clifford.cx_gate_circuits_nondeterministic, ref_2q_clifford.cx_gate_counts_nondeterministic), | ||
(ref_2q_clifford.cz_gate_circuits_nondeterministic, ref_2q_clifford.cz_gate_counts_nondeterministic), | ||
(ref_2q_clifford.swap_gate_circuits_nondeterministic, ref_2q_clifford.swap_gate_counts_nondeterministic)] | ||
|
||
nondeterm_test_list = {'list':nondeterministic_tests, 'shots':2000, 'delta':0.05} | ||
|
||
test_list = [determ_test_list, nondeterm_test_list] | ||
for list in test_list: | ||
for test in list['list']: | ||
delta = list['delta'] | ||
shots = list['shots'] | ||
|
||
circuits = test[0](final_measure=True) | ||
targets = test[1](shots) | ||
job = execute(circuits, QasmSimulator(), backend_options=self.BACKEND_OPTS, shots=shots) | ||
result = job.result() | ||
self.is_completed(result) | ||
self.compare_counts(result, circuits, targets, delta = delta*shots) | ||
|
Oops, something went wrong.