Skip to content

Commit

Permalink
Improve opflow test repeatability (qiskit-community#1147)
Browse files Browse the repository at this point in the history
* Improve opflow test repeatability
  • Loading branch information
woodsp-ibm authored Jul 29, 2020
1 parent 956d754 commit ec502dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
5 changes: 4 additions & 1 deletion test/aqua/operators/test_aer_pauli_expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import itertools
import numpy as np

from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import (X, Y, Z, I, CX, H, S,
ListOp, Zero, One, Plus, Minus, StateFn,
AerPauliExpectation, CircuitSampler)
Expand All @@ -34,8 +35,10 @@ class TestAerPauliExpectation(QiskitAquaTestCase):

def setUp(self) -> None:
super().setUp()
self.seed = 97
backend = Aer.get_backend('qasm_simulator')
self.sampler = CircuitSampler(backend, attach_results=True)
q_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed)
self.sampler = CircuitSampler(q_instance, attach_results=True)
self.expect = AerPauliExpectation()

def test_pauli_expect_pair(self):
Expand Down
5 changes: 4 additions & 1 deletion test/aqua/operators/test_matrix_expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import itertools
import numpy as np

from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import (X, Y, Z, I, CX, H, S,
ListOp, Zero, One, Plus, Minus, StateFn,
MatrixExpectation, CircuitSampler)
Expand All @@ -33,8 +34,10 @@ class TestMatrixExpectation(QiskitAquaTestCase):

def setUp(self) -> None:
super().setUp()
self.seed = 97
backend = BasicAer.get_backend('statevector_simulator')
self.sampler = CircuitSampler(backend, attach_results=True)
q_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed)
self.sampler = CircuitSampler(q_instance, attach_results=True)
self.expect = MatrixExpectation()

def test_pauli_expect_pair(self):
Expand Down
13 changes: 10 additions & 3 deletions test/aqua/operators/test_pauli_expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import itertools
import numpy as np

from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import (X, Y, Z, I, CX, H, S,
ListOp, Zero, One, Plus, Minus, StateFn,
PauliExpectation, CircuitSampler)
Expand All @@ -35,8 +36,10 @@ class TestPauliExpectation(QiskitAquaTestCase):

def setUp(self) -> None:
super().setUp()
self.seed = 97
backend = BasicAer.get_backend('qasm_simulator')
self.sampler = CircuitSampler(backend, attach_results=True)
q_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed)
self.sampler = CircuitSampler(q_instance, attach_results=True)
self.expect = PauliExpectation()

def test_pauli_expect_pair(self):
Expand Down Expand Up @@ -164,7 +167,9 @@ def test_grouped_pauli_expectation(self):
self.assertEqual(num_circuits_ungrouped, 5)

expect_op_grouped = PauliExpectation(group_paulis=True).convert(~StateFn(two_qubit_H2) @ wf)
sampler = CircuitSampler(BasicAer.get_backend('statevector_simulator'))
q_instance = QuantumInstance(BasicAer.get_backend('statevector_simulator'),
seed_simulator=self.seed, seed_transpiler=self.seed)
sampler = CircuitSampler(q_instance)
sampler._extract_circuitstatefns(expect_op_grouped)
num_circuits_grouped = len(sampler._circuit_ops_cache)
self.assertEqual(num_circuits_grouped, 2)
Expand All @@ -175,6 +180,8 @@ def test_ibmq_grouped_pauli_expectation(self):
from qiskit import IBMQ
p = IBMQ.load_account()
backend = p.get_backend('ibmq_qasm_simulator')
q_instance = QuantumInstance(backend, seed_simulator=self.seed, seed_transpiler=self.seed)

paulis_op = ListOp([X, Y, Z, I])
states_op = ListOp([One, Zero, Plus, Minus])

Expand All @@ -183,7 +190,7 @@ def test_ibmq_grouped_pauli_expectation(self):
[-1, 1, 0, -0],
[+1, 1, 1, 1]]
converted_meas = self.expect.convert(~StateFn(paulis_op) @ states_op)
sampled = CircuitSampler(backend).convert(converted_meas)
sampled = CircuitSampler(q_instance).convert(converted_meas)
np.testing.assert_array_almost_equal(sampled.eval(), valids, decimal=1)

def test_multi_representation_ops(self):
Expand Down
8 changes: 6 additions & 2 deletions test/aqua/operators/test_state_op_meas_evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

import unittest
from test.aqua import QiskitAquaTestCase

import numpy

from qiskit import Aer
from qiskit.aqua import QuantumInstance
from qiskit.aqua.operators import StateFn, Zero, One, H, X, I, Z, Plus, Minus, CircuitSampler


Expand Down Expand Up @@ -66,15 +69,16 @@ def test_coefficients_correctly_propagated(self):
self.assertEqual((~StateFn(op) @ state).eval(), 0j)

backend = Aer.get_backend('qasm_simulator')
q_instance = QuantumInstance(backend, seed_simulator=97, seed_transpiler=97)
op = I
with self.subTest('zero coeff in summed StateFn and CircuitSampler'):
state = 0 * (Plus + Minus)
sampler = CircuitSampler(backend).convert(~StateFn(op) @ state)
sampler = CircuitSampler(q_instance).convert(~StateFn(op) @ state)
self.assertEqual(sampler.eval(), 0j)

with self.subTest('coeff gets squared in CircuitSampler shot-based readout'):
state = (Plus + Minus) / numpy.sqrt(2)
sampler = CircuitSampler(backend).convert(~StateFn(op) @ state)
sampler = CircuitSampler(q_instance).convert(~StateFn(op) @ state)
self.assertAlmostEqual(sampler.eval(), 1+0j)


Expand Down

0 comments on commit ec502dd

Please sign in to comment.