@@ -145,7 +145,10 @@ def test_estimator_run_v1(self, backend, abelian_grouping):
145145 ):
146146 pm = generate_preset_pass_manager (optimization_level = 0 , backend = backend )
147147 psi1 , psi2 = pm .run ([psi1 , psi2 ])
148- estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
148+ with self .assertWarns (DeprecationWarning ):
149+ # When BackendEstimatorV2 is called with a backend V1, it raises a
150+ # DeprecationWarning from PassManagerConfig.from_backend
151+ estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
149152 estimator .options .abelian_grouping = abelian_grouping
150153 # Specify the circuit and observable by indices.
151154 # calculate [ <psi1(theta1)|H1|psi1(theta1)> ]
@@ -235,7 +238,10 @@ def test_estimator_with_pub_v1(self, backend, abelian_grouping):
235238 bind2 = BindingsArray .coerce ({tuple (psi2 .parameters ): theta2 })
236239 pub2 = EstimatorPub (psi2 , obs2 , bind2 )
237240
238- estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
241+ with self .assertWarns (DeprecationWarning ):
242+ # When BackendEstimatorV2 is called with a backend V1, it raises a
243+ # DeprecationWarning from PassManagerConfig.from_backend
244+ estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
239245 estimator .options .abelian_grouping = abelian_grouping
240246 result4 = estimator .run ([pub1 , pub2 ]).result ()
241247 np .testing .assert_allclose (result4 [0 ].data .evs , [1.55555728 , - 1.08766318 ], rtol = self ._rtol )
@@ -264,7 +270,10 @@ def test_estimator_run_no_params_v1(self, backend, abelian_grouping):
264270 ):
265271 pm = generate_preset_pass_manager (optimization_level = 0 , backend = backend )
266272 circuit = pm .run (circuit )
267- est = BackendEstimatorV2 (backend = backend , options = self ._options )
273+ with self .assertWarns (DeprecationWarning ):
274+ # When BackendEstimatorV2 is called with a backend V1, it raises a
275+ # DeprecationWarning from PassManagerConfig.from_backend
276+ est = BackendEstimatorV2 (backend = backend , options = self ._options )
268277 est .options .abelian_grouping = abelian_grouping
269278 observable = self .observable .apply_layout (circuit .layout )
270279 result = est .run ([(circuit , observable )]).result ()
@@ -331,7 +340,13 @@ def test_run_single_circuit_observable(self, backend, abelian_grouping):
331340 @combine (backend = BACKENDS_V1 , abelian_grouping = [True , False ])
332341 def test_run_single_circuit_observable_v1 (self , backend , abelian_grouping ):
333342 """Test for single circuit and single observable case."""
334- est = BackendEstimatorV2 (backend = backend , options = self ._options )
343+ with self .assertWarnsRegex (
344+ DeprecationWarning ,
345+ expected_regex = r"The method PassManagerConfig\.from_backend will stop supporting inputs of "
346+ "type `BackendV1`" ,
347+ ):
348+ # BackendEstimatorV2 wont allow BackendV1
349+ est = BackendEstimatorV2 (backend = backend , options = self ._options )
335350 est .options .abelian_grouping = abelian_grouping
336351 with self .assertWarnsRegex (
337352 DeprecationWarning ,
@@ -438,7 +453,11 @@ def test_run_1qubit_v1(self, backend, abelian_grouping):
438453 op = SparsePauliOp .from_list ([("I" , 1 )])
439454 op2 = SparsePauliOp .from_list ([("Z" , 1 )])
440455
441- est = BackendEstimatorV2 (backend = backend , options = self ._options )
456+ with self .assertWarns (DeprecationWarning ):
457+ # When BackendEstimatorV2 is called with a backend V1, it raises a
458+ # DeprecationWarning from PassManagerConfig.from_backend
459+ est = BackendEstimatorV2 (backend = backend , options = self ._options )
460+
442461 est .options .abelian_grouping = abelian_grouping
443462 op_1 = op .apply_layout (qc .layout )
444463 result = est .run ([(qc , op_1 )]).result ()
@@ -513,7 +532,10 @@ def test_run_2qubits_v1(self, backend, abelian_grouping):
513532 op2 = SparsePauliOp .from_list ([("ZI" , 1 )])
514533 op3 = SparsePauliOp .from_list ([("IZ" , 1 )])
515534
516- est = BackendEstimatorV2 (backend = backend , options = self ._options )
535+ with self .assertWarns (DeprecationWarning ):
536+ # When BackendEstimatorV2 is called with a backend V1, it raises a
537+ # DeprecationWarning from PassManagerConfig.from_backend
538+ est = BackendEstimatorV2 (backend = backend , options = self ._options )
517539 est .options .abelian_grouping = abelian_grouping
518540 op_1 = op .apply_layout (qc .layout )
519541 result = est .run ([(qc , op_1 )]).result ()
@@ -539,7 +561,47 @@ def test_run_2qubits_v1(self, backend, abelian_grouping):
539561 result = est .run ([(qc2 , op_6 )]).result ()
540562 np .testing .assert_allclose (result [0 ].data .evs , [- 1 ], rtol = self ._rtol )
541563
542- @combine (backend = BACKENDS , abelian_grouping = [True , False ])
564+ @combine (backend = BACKENDS_V1 , abelian_grouping = [True , False ])
565+ def test_run_errors_v1 (self , backend , abelian_grouping ):
566+ """Test for errors.
567+ To be removed once BackendV1 is removed."""
568+ qc = QuantumCircuit (1 )
569+ qc2 = QuantumCircuit (2 )
570+
571+ op = SparsePauliOp .from_list ([("I" , 1 )])
572+ op2 = SparsePauliOp .from_list ([("II" , 1 )])
573+ with self .assertWarns (DeprecationWarning ):
574+ # When BackendEstimatorV2 is called with a backend V1, it raises a
575+ # DeprecationWarning from PassManagerConfig.from_backend
576+ est = BackendEstimatorV2 (backend = backend , options = self ._options )
577+ est .options .abelian_grouping = abelian_grouping
578+ with self .assertRaises (ValueError ):
579+ est .run ([(qc , op2 )]).result ()
580+ with self .assertRaises (ValueError ):
581+ est .run ([(qc , op , [[1e4 ]])]).result ()
582+ with self .assertRaises (ValueError ):
583+ est .run ([(qc2 , op2 , [[1 , 2 ]])]).result ()
584+ with self .assertRaises (ValueError ):
585+ est .run ([(qc , [op , op2 ], [[1 ]])]).result ()
586+ with self .assertRaises (ValueError ):
587+ est .run ([(qc , op )], precision = - 1 ).result ()
588+ with self .assertRaises (ValueError ):
589+ est .run ([(qc , 1j * op )], precision = 0.1 ).result ()
590+ # precision == 0
591+ with self .assertRaises (ValueError ):
592+ est .run ([(qc , op , None , 0 )]).result ()
593+ with self .assertRaises (ValueError ):
594+ est .run ([(qc , op )], precision = 0 ).result ()
595+ # precision < 0
596+ with self .assertRaises (ValueError ):
597+ est .run ([(qc , op , None , - 1 )]).result ()
598+ with self .assertRaises (ValueError ):
599+ est .run ([(qc , op )], precision = - 1 ).result ()
600+ with self .subTest ("missing []" ):
601+ with self .assertRaisesRegex (ValueError , "An invalid Estimator pub-like was given" ):
602+ _ = est .run ((qc , op )).result ()
603+
604+ @combine (backend = BACKENDS_V2 , abelian_grouping = [True , False ])
543605 def test_run_errors (self , backend , abelian_grouping ):
544606 """Test for errors"""
545607 qc = QuantumCircuit (1 )
@@ -624,7 +686,13 @@ def test_run_numpy_params_v1(self, backend, abelian_grouping):
624686 statevector_estimator = StatevectorEstimator (seed = 123 )
625687 target = statevector_estimator .run ([(qc , op , params_list )]).result ()
626688
627- backend_estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
689+ with self .assertWarnsRegex (
690+ DeprecationWarning ,
691+ expected_regex = r"The method PassManagerConfig\.from_backend will stop supporting inputs of "
692+ "type `BackendV1`" ,
693+ ):
694+ # BackendEstimatorV2 wont allow BackendV1
695+ backend_estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
628696 backend_estimator .options .abelian_grouping = abelian_grouping
629697
630698 with self .subTest ("ndarrary" ):
@@ -662,7 +730,10 @@ def test_precision(self, backend, abelian_grouping):
662730 @combine (backend = BACKENDS_V1 , abelian_grouping = [True , False ])
663731 def test_precision_v1 (self , backend , abelian_grouping ):
664732 """Test for precision"""
665- estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
733+ with self .assertWarns (DeprecationWarning ):
734+ # When BackendEstimatorV2 is called with a backend V1, it raises a
735+ # DeprecationWarning from PassManagerConfig.from_backend
736+ estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
666737 estimator .options .abelian_grouping = abelian_grouping
667738 with self .assertWarnsRegex (
668739 DeprecationWarning ,
@@ -705,7 +776,10 @@ def test_diff_precision(self, backend, abelian_grouping):
705776 @combine (backend = BACKENDS_V1 , abelian_grouping = [True , False ])
706777 def test_diff_precision_v1 (self , backend , abelian_grouping ):
707778 """Test for running different precisions at once"""
708- estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
779+ with self .assertWarns (DeprecationWarning ):
780+ # When BackendEstimatorV2 is called with a backend V1, it raises a
781+ # DeprecationWarning from PassManagerConfig.from_backend
782+ estimator = BackendEstimatorV2 (backend = backend , options = self ._options )
709783 estimator .options .abelian_grouping = abelian_grouping
710784 with self .assertWarnsRegex (
711785 DeprecationWarning ,
@@ -792,7 +866,10 @@ def test_job_size_limit_backend_v1(self):
792866 op = SparsePauliOp .from_list ([("IZ" , 1 ), ("XI" , 2 ), ("ZY" , - 1 )])
793867 k = 5
794868 param_list = self ._rng .random (qc .num_parameters ).tolist ()
795- estimator = BackendEstimatorV2 (backend = backend )
869+ with self .assertWarns (DeprecationWarning ):
870+ # When BackendEstimatorV2 is called with a backend V1, it raises a
871+ # DeprecationWarning from PassManagerConfig.from_backend
872+ estimator = BackendEstimatorV2 (backend = backend )
796873 with patch .object (backend , "run" ) as run_mock :
797874 estimator .run ([(qc , op , param_list )] * k ).result ()
798875 self .assertEqual (run_mock .call_count , 10 )
0 commit comments