1313"""Tests for QuantumRandomAccessEncoding"""
1414import itertools
1515import unittest
16- from test . optimization_test_case import QiskitOptimizationTestCase
16+ from test import QiskitOptimizationTestCase
1717
18- from ddt import ddt , data , unpack
19- import numpy as np
2018import networkx as nx
21-
19+ import numpy as np
20+ from ddt import data , ddt , unpack
2221from qiskit .circuit import QuantumCircuit
23- from qiskit .primitives import Estimator
22+ from qiskit .primitives import Estimator , StatevectorEstimator
2423from qiskit .quantum_info import SparsePauliOp
2524
2625from qiskit_optimization .algorithms .qrao import (
2726 EncodingCommutationVerifier ,
2827 QuantumRandomAccessEncoding ,
2928)
30- from qiskit_optimization .problems import QuadraticProgram , QuadraticObjective
3129from qiskit_optimization .applications import Maxcut
30+ from qiskit_optimization .problems import QuadraticObjective , QuadraticProgram
3231
3332
3433class TestQuantumRandomAccessEncoding (QiskitOptimizationTestCase ):
@@ -252,17 +251,20 @@ def test_qrac_unsupported_encoding(self):
252251class TestEncodingCommutationVerifier (QiskitOptimizationTestCase ):
253252 """Tests for EncodingCommutationVerifier."""
254253
255- def check_problem_commutation (self , problem : QuadraticProgram , max_vars_per_qubit : int ):
254+ def check_problem_commutation (
255+ self , problem : QuadraticProgram , max_vars_per_qubit : int , version : str
256+ ):
256257 """Utility function to check that the problem commutes with its encoding"""
257258 encoding = QuantumRandomAccessEncoding (max_vars_per_qubit = max_vars_per_qubit )
258259 encoding .encode (problem )
259- estimator = Estimator ()
260+ estimator = Estimator () if version == "v1" else StatevectorEstimator ()
260261 verifier = EncodingCommutationVerifier (encoding , estimator )
261262 self .assertEqual (len (verifier ), 2 ** encoding .num_vars )
262263 for _ , obj_val , encoded_obj_val in verifier :
263264 np .testing .assert_allclose (obj_val , encoded_obj_val , atol = 1e-5 )
264265
265- def test_encoding_commutation_verifier (self ):
266+ @data ("v1" , "v2" )
267+ def test_encoding_commutation_verifier (self , version ):
266268 """Test EncodingCommutationVerifier"""
267269 problem = QuadraticProgram ()
268270 problem .binary_var ("x" )
@@ -272,11 +274,11 @@ def test_encoding_commutation_verifier(self):
272274
273275 encoding = QuantumRandomAccessEncoding (max_vars_per_qubit = 3 )
274276 encoding .encode (problem )
275- self .check_problem_commutation (problem , 3 )
277+ self .check_problem_commutation (problem , 3 , version )
276278
277- @data (* itertools .product ([1 , 2 , 3 ], ["minimize" , "maximize" ]))
279+ @data (* itertools .product ([1 , 2 , 3 ], ["minimize" , "maximize" ], [ "v1" , "v2" ] ))
278280 @unpack
279- def test_one_qubit_qrac (self , max_vars_per_qubit , task ):
281+ def test_one_qubit_qrac (self , max_vars_per_qubit , task , version ):
280282 """Test commutation of single qubit QRAC with non-uniform weights, degree 1 terms"""
281283
282284 problem = QuadraticProgram ()
@@ -287,15 +289,17 @@ def test_one_qubit_qrac(self, max_vars_per_qubit, task):
287289 problem .minimize (linear = obj )
288290 else :
289291 problem .maximize (linear = obj )
290- self .check_problem_commutation (problem , max_vars_per_qubit )
292+ self .check_problem_commutation (problem , max_vars_per_qubit , version )
291293
292294 @data (
293295 * itertools .product (
294- [1 , 2 , 3 ], [QuadraticObjective .Sense .MINIMIZE , QuadraticObjective .Sense .MAXIMIZE ]
296+ [1 , 2 , 3 ],
297+ [QuadraticObjective .Sense .MINIMIZE , QuadraticObjective .Sense .MAXIMIZE ],
298+ ["v1" , "v2" ],
295299 )
296300 )
297301 @unpack
298- def test_uniform_weights_degree_2 (self , max_vars_per_qubit , task ):
302+ def test_uniform_weights_degree_2 (self , max_vars_per_qubit , task , version ):
299303 """Test problem commutation with degree 2 terms"""
300304 # Note that the variable embedding has some qubits with 1, 2, and 3 qubits
301305 elist = [(0 , 1 ), (1 , 2 ), (2 , 3 ), (3 , 4 ), (4 , 5 ), (5 , 0 ), (0 , 3 ), (1 , 4 ), (2 , 4 )]
@@ -306,25 +310,27 @@ def test_uniform_weights_degree_2(self, max_vars_per_qubit, task):
306310 maxcut = Maxcut (graph )
307311 problem = maxcut .to_quadratic_program ()
308312 problem .objective .sense = task
309- self .check_problem_commutation (problem , max_vars_per_qubit )
313+ self .check_problem_commutation (problem , max_vars_per_qubit , version )
310314
311- @data (1 , 2 , 3 )
312- def test_random_unweighted_maxcut (self , max_vars_per_qubit ):
315+ @data (* itertools .product ([1 , 2 , 3 ], ["v1" , "v2" ]))
316+ @unpack
317+ def test_random_unweighted_maxcut (self , max_vars_per_qubit , version ):
313318 """Test problem commutation with random unweighted MaxCut"""
314319 graph = nx .random_regular_graph (3 , 8 )
315320 maxcut = Maxcut (graph )
316321 problem = maxcut .to_quadratic_program ()
317- self .check_problem_commutation (problem , max_vars_per_qubit )
322+ self .check_problem_commutation (problem , max_vars_per_qubit , version )
318323
319- @data (1 , 2 , 3 )
320- def test_random_weighted_maxcut (self , max_vars_per_qubit ):
324+ @data (* itertools .product ([1 , 2 , 3 ], ["v1" , "v2" ]))
325+ @unpack
326+ def test_random_weighted_maxcut (self , max_vars_per_qubit , version ):
321327 """Test problem commutation with random weighted MaxCut"""
322328 graph = nx .random_regular_graph (3 , 8 )
323329 for w , v in graph .edges :
324330 graph [w ][v ]["weight" ] = np .random .randint (1 , 10 )
325331 maxcut = Maxcut (graph )
326332 problem = maxcut .to_quadratic_program ()
327- self .check_problem_commutation (problem , max_vars_per_qubit )
333+ self .check_problem_commutation (problem , max_vars_per_qubit , version )
328334
329335
330336if __name__ == "__main__" :
0 commit comments