@@ -113,12 +113,12 @@ def test_estimator_run_no_params(self, backend):
113
113
self .assertIsInstance (result , EstimatorResult )
114
114
np .testing .assert_allclose (result .values , [- 1.284366511861733 ], rtol = 0.05 )
115
115
116
- @combine (backend = BACKENDS )
117
- def test_run_1qubit (self , backend ):
116
+ @combine (backend = BACKENDS , creg = [ True , False ] )
117
+ def test_run_1qubit (self , backend , creg ):
118
118
"""Test for 1-qubit cases"""
119
119
backend .set_options (seed_simulator = 123 )
120
- qc = QuantumCircuit (1 )
121
- qc2 = QuantumCircuit (1 )
120
+ qc = QuantumCircuit (1 , 1 ) if creg else QuantumCircuit ( 1 )
121
+ qc2 = QuantumCircuit (1 , 1 ) if creg else QuantumCircuit ( 1 )
122
122
qc2 .x (0 )
123
123
124
124
op = SparsePauliOp .from_list ([("I" , 1 )])
@@ -141,12 +141,12 @@ def test_run_1qubit(self, backend):
141
141
self .assertIsInstance (result , EstimatorResult )
142
142
np .testing .assert_allclose (result .values , [- 1 ], rtol = 0.1 )
143
143
144
- @combine (backend = BACKENDS )
145
- def test_run_2qubits (self , backend ):
144
+ @combine (backend = BACKENDS , creg = [ True , False ] )
145
+ def test_run_2qubits (self , backend , creg ):
146
146
"""Test for 2-qubit cases (to check endian)"""
147
147
backend .set_options (seed_simulator = 123 )
148
- qc = QuantumCircuit (2 )
149
- qc2 = QuantumCircuit (2 )
148
+ qc = QuantumCircuit (2 , 1 ) if creg else QuantumCircuit ( 2 )
149
+ qc2 = QuantumCircuit (2 , 1 ) if creg else QuantumCircuit ( 2 , 1 )
150
150
qc2 .x (0 )
151
151
152
152
op = SparsePauliOp .from_list ([("II" , 1 )])
@@ -191,8 +191,6 @@ def test_run_errors(self, backend):
191
191
est = BackendEstimator (backend = backend )
192
192
with self .assertRaises (ValueError ):
193
193
est .run ([qc ], [op2 ], [[]]).result ()
194
- with self .assertRaises (ValueError ):
195
- est .run ([qc2 ], [op ], [[]]).result ()
196
194
with self .assertRaises (ValueError ):
197
195
est .run ([qc ], [op ], [[1e4 ]]).result ()
198
196
with self .assertRaises (ValueError ):
@@ -270,10 +268,6 @@ def max_circuits(self):
270
268
271
269
backend = FakeNairobiLimitedCircuits ()
272
270
backend .set_options (seed_simulator = 123 )
273
- qc = QuantumCircuit (1 )
274
- qc2 = QuantumCircuit (1 )
275
- qc2 .x (0 )
276
- backend .set_options (seed_simulator = 123 )
277
271
qc = RealAmplitudes (num_qubits = 2 , reps = 2 )
278
272
op = SparsePauliOp .from_list ([("IZ" , 1 ), ("XI" , 2 ), ("ZY" , - 1 )])
279
273
k = 5
@@ -385,6 +379,45 @@ def test_layout(self, backend):
385
379
else :
386
380
self .assertEqual (value , - 1 )
387
381
382
+ @unittest .skipUnless (optionals .HAS_AER , "qiskit-aer is required to run this test" )
383
+ def test_circuit_with_measurement (self ):
384
+ """Test estimator with a dynamic circuit"""
385
+ from qiskit_aer import AerSimulator
386
+
387
+ bell = QuantumCircuit (2 )
388
+ bell .h (0 )
389
+ bell .cx (0 , 1 )
390
+ bell .measure_all ()
391
+ observable = SparsePauliOp ("ZZ" )
392
+
393
+ backend = AerSimulator ()
394
+ backend .set_options (seed_simulator = 15 )
395
+ estimator = BackendEstimator (backend , skip_transpilation = True )
396
+ estimator .set_transpile_options (seed_transpiler = 15 )
397
+ result = estimator .run (bell , observable ).result ()
398
+ self .assertAlmostEqual (result .values [0 ], 1 , places = 1 )
399
+
400
+ @unittest .skipUnless (optionals .HAS_AER , "qiskit-aer is required to run this test" )
401
+ def test_dynamic_circuit (self ):
402
+ """Test estimator with a dynamic circuit"""
403
+ from qiskit_aer import AerSimulator
404
+
405
+ qc = QuantumCircuit (2 , 1 )
406
+ with qc .for_loop (range (5 )):
407
+ qc .h (0 )
408
+ qc .cx (0 , 1 )
409
+ qc .measure (1 , 0 )
410
+ qc .break_loop ().c_if (0 , True )
411
+
412
+ observable = SparsePauliOp ("IZ" )
413
+
414
+ backend = AerSimulator ()
415
+ backend .set_options (seed_simulator = 15 )
416
+ estimator = BackendEstimator (backend , skip_transpilation = True )
417
+ estimator .set_transpile_options (seed_transpiler = 15 )
418
+ result = estimator .run (qc , observable ).result ()
419
+ self .assertAlmostEqual (result .values [0 ], 0 , places = 1 )
420
+
388
421
389
422
if __name__ == "__main__" :
390
423
unittest .main ()
0 commit comments