Skip to content

Commit 196f168

Browse files
authored
Fix to WeightedPauliOperator (qiskit-community/qiskit-aqua#891)
1 parent b3d82b9 commit 196f168

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

test/aqua/operators/test_weighted_pauli_operator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,39 @@ def test_evolve(self, expansion_mode, evo_time, num_time_slices):
578578
self.log.debug('The fidelity between matrix and circuit: %s', f_mc)
579579
self.assertAlmostEqual(f_mc, 1)
580580

581+
def test_simplification(self):
582+
""" Test Hamiltonians produce same result after simplification by constructor """
583+
q = QuantumRegister(2, name='q')
584+
qc = QuantumCircuit(q)
585+
qc.rx(10.9891251356965, 0)
586+
qc.rx(6.286692023269373, 1)
587+
qc.rz(7.848801398269382, 0)
588+
qc.rz(9.42477796076938, 1)
589+
qc.cx(0, 1)
590+
591+
def eval_op(op):
592+
from qiskit import execute
593+
backend = BasicAer.get_backend('qasm_simulator')
594+
evaluation_circuits = op.construct_evaluation_circuit(qc, False)
595+
job = execute(evaluation_circuits, backend, shots=1024)
596+
return op.evaluate_with_result(job.result(), False)
597+
598+
pauli_string = [[1.0, Pauli.from_label('XX')],
599+
[-1.0, Pauli.from_label('YY')],
600+
[-1.0, Pauli.from_label('ZZ')]]
601+
wpo = WeightedPauliOperator(pauli_string)
602+
expectation_value, _ = eval_op(wpo)
603+
self.assertAlmostEqual(expectation_value, -3.0, places=2)
604+
605+
# Half each coefficient value but double up (6 Paulis total)
606+
pauli_string = [[0.5, Pauli.from_label('XX')],
607+
[-0.5, Pauli.from_label('YY')],
608+
[-0.5, Pauli.from_label('ZZ')]]
609+
pauli_string *= 2
610+
wpo2 = WeightedPauliOperator(pauli_string)
611+
expectation_value, _ = eval_op(wpo2)
612+
self.assertAlmostEqual(expectation_value, -3.0, places=2)
613+
581614

582615
if __name__ == '__main__':
583616
unittest.main()

0 commit comments

Comments
 (0)