@@ -578,6 +578,39 @@ def test_evolve(self, expansion_mode, evo_time, num_time_slices):
578
578
self .log .debug ('The fidelity between matrix and circuit: %s' , f_mc )
579
579
self .assertAlmostEqual (f_mc , 1 )
580
580
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
+
581
614
582
615
if __name__ == '__main__' :
583
616
unittest .main ()
0 commit comments