From f76a88fb4d3f85be3059cc737cfc4127909edeed Mon Sep 17 00:00:00 2001 From: Ikko Hamamura Date: Sat, 31 Oct 2020 10:05:55 +0900 Subject: [PATCH] Fix opflow bugs (Qiskit/qiskit-aqua#1390) * fix Qiskit/qiskit-aqua#1311 * fix Qiskit/qiskit-aqua#1317 * add a test of reps for trotterization * fix tests for Qiskit/qiskit-aqua#1311 * fix typing * fix Qiskit/qiskit-aqua#1321 * add release note * fix Qiskit/qiskit-aqua#1381 * add a test for to_opflow * Update qiskit/aqua/operators/converters/circuit_sampler.py Co-authored-by: John Lapeyre * remove None from constructor * simplify logic * Revert "remove None from constructor" This reverts commit c884ebba123adee22f252e11f90964a6defaec38. * add releasenote * simplify logic (not iscomplex to isreal) * remove Optional * Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py * Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py * Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py Co-authored-by: John Lapeyre Co-authored-by: Panagiotis Barkoutsos Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com> --- .../legacy/test_weighted_pauli_operator.py | 17 +++++++++++++++++ test/aqua/operators/test_evolution.py | 18 ++++++++++++++++-- test/aqua/operators/test_state_construction.py | 8 ++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/test/aqua/operators/legacy/test_weighted_pauli_operator.py b/test/aqua/operators/legacy/test_weighted_pauli_operator.py index 09a78730fb0..b7f4a07bfb5 100644 --- a/test/aqua/operators/legacy/test_weighted_pauli_operator.py +++ b/test/aqua/operators/legacy/test_weighted_pauli_operator.py @@ -25,6 +25,7 @@ from qiskit.aqua.operators import WeightedPauliOperator from qiskit.aqua.operators.legacy import op_converter from qiskit.aqua.components.initial_states import Custom +from qiskit.aqua.operators import I, X, Y, Z @ddt @@ -610,6 +611,22 @@ def eval_op(op): expectation_value, _ = eval_op(wpo2) self.assertAlmostEqual(expectation_value, -3.0, places=2) + def test_to_opflow(self): + """Test for to_opflow() in WeightedPauliOperator""" + pauli_a = 'IXYZ' + pauli_b = 'ZYIX' + coeff_a = 0.5 + 1j + coeff_b = -0.5 - 1j + pauli_term_a = [coeff_a, Pauli.from_label(pauli_a)] + pauli_term_b = [coeff_b, Pauli.from_label(pauli_b)] + op_a = WeightedPauliOperator(paulis=[pauli_term_a]) + op_b = WeightedPauliOperator(paulis=[pauli_term_b]) + legacy_op = op_a + op_b + + op = coeff_a * (I ^ X ^ Y ^ Z) + coeff_b * (Z ^ Y ^ I ^ X) + + self.assertEqual(op, legacy_op.to_opflow()) + if __name__ == '__main__': unittest.main() diff --git a/test/aqua/operators/test_evolution.py b/test/aqua/operators/test_evolution.py index 92ffab0eea4..a962739fa60 100644 --- a/test/aqua/operators/test_evolution.py +++ b/test/aqua/operators/test_evolution.py @@ -22,11 +22,11 @@ from qiskit.circuit import ParameterVector, Parameter from qiskit.aqua.operators import (X, Y, Z, I, CX, H, ListOp, CircuitOp, Zero, EvolutionFactory, - EvolvedOp, PauliTrotterEvolution, QDrift) - + EvolvedOp, PauliTrotterEvolution, QDrift, Trotter, Suzuki) # pylint: disable=invalid-name + class TestEvolution(QiskitAquaTestCase): """Evolution tests.""" @@ -255,6 +255,20 @@ def test_mixed_evolution(self): for p in thetas[1:]: self.assertNotIn(p, circuit_params) + def test_reps(self): + """Test reps and order params in Trotterization""" + reps = 7 + trotter = Trotter(reps=reps) + self.assertEqual(trotter.reps, reps) + + order = 5 + suzuki = Suzuki(reps=reps, order=order) + self.assertEqual(suzuki.reps, reps) + self.assertEqual(suzuki.order, order) + + qdrift = QDrift(reps=reps) + self.assertEqual(qdrift.reps, reps) + if __name__ == '__main__': unittest.main() diff --git a/test/aqua/operators/test_state_construction.py b/test/aqua/operators/test_state_construction.py index fbb09e5adf3..639fc6c25ab 100644 --- a/test/aqua/operators/test_state_construction.py +++ b/test/aqua/operators/test_state_construction.py @@ -146,10 +146,10 @@ def test_circuit_state_fn_from_complex_vector_initialize(self): def test_sampling(self): """ state fn circuit from dict initialize test """ - statedict = {'101': .5, - '100': .1, - '000': .2, - '111': .5} + statedict = {'101': .5 + 1.j, + '100': .1 + 2.j, + '000': .2 + 0.j, + '111': .5 + 1.j} sfc = CircuitStateFn.from_dict(statedict) circ_samples = sfc.sample() dict_samples = StateFn(statedict).sample()