From bcb5fce201fdce266003d76264994f07b99e5948 Mon Sep 17 00:00:00 2001 From: woodsp Date: Tue, 7 Apr 2020 13:16:59 -0400 Subject: [PATCH] Fix to WeightedPauliOperator --- CHANGELOG.md | 4 ++- .../aqua/operators/weighted_pauli_operator.py | 3 +- .../operators/test_weighted_pauli_operator.py | 33 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cfa1d0a53..60ccc74953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,9 @@ Fixed - Boolean logic circuit construction (#819) - Measurement on actual devices for Amplitude Estimation algorithms (#841) (#842) -- Supported constant values on the left-hand side of constraints and variables on the right-hand side of constraints for the DOcplex translator (#750) +- Supported constant values on the left-hand side of constraints and variables on the right-hand + side of constraints for the DOcplex translator (#750) +- WeightedPauliOperator constructor simplification bug (#891) [0.6.5](https://github.com/Qiskit/qiskit-aqua/compare/0.6.4...0.6.5) - 2020-03-16 ================================================================================= diff --git a/qiskit/aqua/operators/weighted_pauli_operator.py b/qiskit/aqua/operators/weighted_pauli_operator.py index d45d352e9c..bdd89988e3 100644 --- a/qiskit/aqua/operators/weighted_pauli_operator.py +++ b/qiskit/aqua/operators/weighted_pauli_operator.py @@ -365,9 +365,8 @@ def simplify(self, copy=False): break for idx in indices: new_idx = old_to_new_indices[idx] - if new_idx is not None: + if new_idx is not None and new_idx not in new_indices: new_indices.append(new_idx) - new_indices = list(set(new_indices)) if new_indices and not found: new_basis.append((basis, new_indices)) op._basis = new_basis diff --git a/test/aqua/operators/test_weighted_pauli_operator.py b/test/aqua/operators/test_weighted_pauli_operator.py index 17626c3510..b92686eeab 100644 --- a/test/aqua/operators/test_weighted_pauli_operator.py +++ b/test/aqua/operators/test_weighted_pauli_operator.py @@ -578,6 +578,39 @@ def test_evolve(self, expansion_mode, evo_time, num_time_slices): self.log.debug('The fidelity between matrix and circuit: %s', f_mc) self.assertAlmostEqual(f_mc, 1) + def test_simplification(self): + """ Test Hamiltonians produce same result after simplification by constructor """ + q = QuantumRegister(2, name='q') + qc = QuantumCircuit(q) + qc.rx(10.9891251356965, 0) + qc.rx(6.286692023269373, 1) + qc.rz(7.848801398269382, 0) + qc.rz(9.42477796076938, 1) + qc.cx(0, 1) + + def eval_op(op): + from qiskit import execute + backend = BasicAer.get_backend('qasm_simulator') + evaluation_circuits = op.construct_evaluation_circuit(qc, False) + job = execute(evaluation_circuits, backend, shots=1024) + return op.evaluate_with_result(job.result(), False) + + pauli_string = [[1.0, Pauli.from_label('XX')], + [-1.0, Pauli.from_label('YY')], + [-1.0, Pauli.from_label('ZZ')]] + wpo = WeightedPauliOperator(pauli_string) + expectation_value, _ = eval_op(wpo) + self.assertAlmostEqual(expectation_value, -3.0, places=2) + + # Half each coefficient value but double up (6 Paulis total) + pauli_string = [[0.5, Pauli.from_label('XX')], + [-0.5, Pauli.from_label('YY')], + [-0.5, Pauli.from_label('ZZ')]] + pauli_string *= 2 + wpo2 = WeightedPauliOperator(pauli_string) + expectation_value, _ = eval_op(wpo2) + self.assertAlmostEqual(expectation_value, -3.0, places=2) + if __name__ == '__main__': unittest.main()