Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Fix to WeightedPauliOperator #891

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=================================================================================
Expand Down
3 changes: 1 addition & 2 deletions qiskit/aqua/operators/weighted_pauli_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/aqua/operators/test_weighted_pauli_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()