32
32
33
33
34
34
class TestEstimatorV2 (QiskitAerTestCase ):
35
- """Test Estimator"""
35
+ """Test Estimator V2 """
36
36
37
37
def setUp (self ):
38
38
super ().setUp ()
@@ -84,6 +84,7 @@ def test_estimator_run(self):
84
84
job = estimator .run ([(psi1 , ham1 , [theta1 ])])
85
85
result = job .result ()
86
86
np .testing .assert_allclose (result [0 ].data .evs , [1.5555572817900956 ], rtol = self ._rtol )
87
+ self .assertIn ("simulator_metadata" , result [0 ].metadata )
87
88
88
89
# Objects can be passed instead of indices.
89
90
# Note that passing objects has an overhead
@@ -93,12 +94,14 @@ def test_estimator_run(self):
93
94
ham1 = hamiltonian1 .apply_layout (psi2 .layout )
94
95
result2 = estimator .run ([(psi2 , ham1 , theta2 )]).result ()
95
96
np .testing .assert_allclose (result2 [0 ].data .evs , [2.97797666 ], rtol = self ._rtol )
97
+ self .assertIn ("simulator_metadata" , result2 [0 ].metadata )
96
98
97
99
# calculate [ <psi1(theta1)|H2|psi1(theta1)>, <psi1(theta1)|H3|psi1(theta1)> ]
98
100
ham2 = hamiltonian2 .apply_layout (psi1 .layout )
99
101
ham3 = hamiltonian3 .apply_layout (psi1 .layout )
100
102
result3 = estimator .run ([(psi1 , [ham2 , ham3 ], theta1 )]).result ()
101
103
np .testing .assert_allclose (result3 [0 ].data .evs , [- 0.551653 , 0.07535239 ], rtol = self ._rtol )
104
+ self .assertIn ("simulator_metadata" , result3 [0 ].metadata )
102
105
103
106
# calculate [ [<psi1(theta1)|H1|psi1(theta1)>,
104
107
# <psi1(theta3)|H3|psi1(theta3)>],
@@ -114,6 +117,8 @@ def test_estimator_run(self):
114
117
).result ()
115
118
np .testing .assert_allclose (result4 [0 ].data .evs , [1.55555728 , - 1.08766318 ], rtol = self ._rtol )
116
119
np .testing .assert_allclose (result4 [1 ].data .evs , [0.17849238 ], rtol = self ._rtol )
120
+ self .assertIn ("simulator_metadata" , result4 [0 ].metadata )
121
+ self .assertIn ("simulator_metadata" , result4 [1 ].metadata )
117
122
118
123
def test_estimator_with_pub (self ):
119
124
"""Test estimator with explicit EstimatorPubs."""
@@ -296,10 +301,9 @@ def test_run_errors(self):
296
301
est .run ([(qc , op , None , - 1 )]).result ()
297
302
with self .assertRaises (ValueError ):
298
303
est .run ([(qc , op )], precision = - 1 ).result ()
299
- # Comment out after Qiskit 1.0.3
300
- # with self.subTest("missing []"):
301
- # with self.assertRaisesRegex(ValueError, "An invalid Estimator pub-like was given"):
302
- # _ = est.run((qc, op)).result()
304
+ with self .subTest ("missing []" ):
305
+ with self .assertRaisesRegex (ValueError , "An invalid Estimator pub-like was given" ):
306
+ _ = est .run ((qc , op )).result ()
303
307
304
308
def test_run_numpy_params (self ):
305
309
"""Test for numpy array as parameter values"""
@@ -347,6 +351,31 @@ def test_precision(self):
347
351
result = job .result ()
348
352
np .testing .assert_allclose (result [0 ].data .evs , [1.5555572817900956 ], rtol = self ._rtol )
349
353
354
+ def test_diff_precision (self ):
355
+ """Test for running different precisions at once"""
356
+ estimator = EstimatorV2 (options = self ._options )
357
+ pm = generate_preset_pass_manager (optimization_level = 0 , backend = self .backend )
358
+ psi1 = pm .run (self .psi [0 ])
359
+ hamiltonian1 = self .hamiltonian [0 ].apply_layout (psi1 .layout )
360
+ theta1 = self .theta [0 ]
361
+ job = estimator .run (
362
+ [(psi1 , hamiltonian1 , [theta1 ]), (psi1 , hamiltonian1 , [theta1 ], self ._precision * 0.8 )]
363
+ )
364
+ result = job .result ()
365
+ np .testing .assert_allclose (result [0 ].data .evs , [1.901141473854881 ], rtol = self ._rtol )
366
+ np .testing .assert_allclose (result [1 ].data .evs , [1.901141473854881 ], rtol = self ._rtol )
367
+
368
+ def test_iter_pub (self ):
369
+ """test for an iterable of pubs"""
370
+ circuit = self .ansatz .assign_parameters ([0 , 1 , 1 , 2 , 3 , 5 ])
371
+ pm = generate_preset_pass_manager (optimization_level = 0 , backend = self .backend )
372
+ circuit = pm .run (circuit )
373
+ estimator = EstimatorV2 (options = self ._options )
374
+ observable = self .observable .apply_layout (circuit .layout )
375
+ result = estimator .run (iter ([(circuit , observable ), (circuit , observable )])).result ()
376
+ np .testing .assert_allclose (result [0 ].data .evs , [- 1.284366511861733 ], rtol = self ._rtol )
377
+ np .testing .assert_allclose (result [1 ].data .evs , [- 1.284366511861733 ], rtol = self ._rtol )
378
+
350
379
351
380
if __name__ == "__main__" :
352
381
unittest .main ()
0 commit comments