26
26
)
27
27
from openqaoa .utilities import X_mixer_hamiltonian
28
28
from openqaoa .problems import NumberPartition
29
- from openqaoa import QAOA
29
+ from openqaoa import QAOA , create_device
30
30
31
31
32
32
class TestingQAOAQiskitQPUBackendAzure (unittest .TestCase ):
33
-
34
- """This Object tests the QAOA Qiskit QPU Backend object with the Azure
33
+
34
+ """This Object tests the QAOA Qiskit QPU Backend object with the Azure
35
35
Device object. This checks that the use of qiskit to send circuits to Azure
36
36
is working as intended.
37
-
37
+
38
38
The Azure CLI has to be configured beforehand to run these tests.
39
39
"""
40
-
40
+
41
41
@pytest .mark .api
42
42
def setUp (self ):
43
-
43
+ """
44
+ Setting up the credentials
45
+ """
44
46
bashCommand = "az resource list"
45
47
process = subprocess .Popen (bashCommand .split (), stdout = subprocess .PIPE )
46
48
output , error = process .communicate ()
47
-
49
+
48
50
if error is not None :
49
51
print (error )
50
- raise Exception ('You must have the Azure CLI installed and must be logged in to use the Azure Quantum Backends' )
52
+ raise Exception (
53
+ "You must have the Azure CLI installed and must be logged in to use the Azure Quantum Backends"
54
+ )
51
55
else :
52
56
output_json = json .loads (output )
53
- output_json_s = [each_json for each_json in output_json if each_json ['name' ] == 'TestingOpenQAOA' ][0 ]
54
- self .RESOURCE_ID = output_json_s ['id' ]
55
- self .AZ_LOCATION = output_json_s ['location' ]
56
-
57
+ output_json_s = [
58
+ each_json
59
+ for each_json in output_json
60
+ if each_json ["name" ] == "TestingOpenQAOA"
61
+ ][0 ]
62
+ self .RESOURCE_ID = output_json_s ["id" ]
63
+ self .AZ_LOCATION = output_json_s ["location" ]
64
+
57
65
@pytest .mark .sim
58
66
def check_shots_tally (self ):
59
-
60
- """ There is a known bug in the qiskit backend for azure where if the shots
61
- argument might be ignored. This test checks that the output from the azure
67
+ """
68
+ There is a known bug in the qiskit backend for azure where if the shots
69
+ argument might be ignored. This test checks that the output from the azure
62
70
computation matches the input requirements.
63
71
"""
64
-
72
+
65
73
shots = 1024
66
74
problem_qubo = NumberPartition ([1 , 2 , 3 ]).qubo
67
- azure_device = create_device (location = 'azure' , name = 'rigetti.sim.qvm' ,
68
- resource_id = self .RESOURCE_ID ,
69
- az_location = self .AZ_LOCATION )
70
-
75
+ azure_device = create_device (
76
+ location = "azure" ,
77
+ name = "rigetti.sim.qvm" ,
78
+ resource_id = self .RESOURCE_ID ,
79
+ az_location = self .AZ_LOCATION ,
80
+ )
81
+
71
82
q = QAOA ()
72
83
q .set_device (azure_device )
73
84
q .set_backend_properties (n_shots = shots )
74
- q .set_classical_optimizer (maxiter = 1 )
85
+ q .set_classical_optimizer (maxiter = 1 )
75
86
q .compile (problem_qubo )
76
87
q .optimize ()
77
-
78
- comp_shots = sum (q .result .optimized ['optimized measurement outcomes' ].values ())
79
-
88
+
89
+ q .dump (file_name = "check_shots_tally" , prepend_id = False , overwrite = True )
90
+
91
+ comp_shots = sum (q .result .optimized ["optimized measurement outcomes" ].values ())
92
+
80
93
self .assertEqual (shots , comp_shots )
81
-
94
+
82
95
@pytest .mark .api
83
96
def test_expectations_in_init (self ):
84
-
85
97
"""
86
98
Testing the Exceptions in the init function of the QiskitQPUShotBasedBackend
87
99
"""
88
-
100
+
89
101
nqubits = 3
90
102
p = 1
91
103
weights = [1 , 1 , 1 ]
92
- gammas = [1 / 8 * np .pi ]
93
- betas = [1 / 8 * np .pi ]
104
+ gammas = [1 / 8 * np .pi ]
105
+ betas = [1 / 8 * np .pi ]
94
106
shots = 10000
95
107
96
- cost_hamil = Hamiltonian ([PauliOp ('ZZ' , (0 , 1 )), PauliOp ('ZZ' , (1 , 2 )),
97
- PauliOp ('ZZ' , (0 , 2 ))], weights , 1 )
108
+ cost_hamil = Hamiltonian (
109
+ [PauliOp ("ZZ" , (0 , 1 )), PauliOp ("ZZ" , (1 , 2 )), PauliOp ("ZZ" , (0 , 2 ))],
110
+ weights ,
111
+ 1 ,
112
+ )
98
113
mixer_hamil = X_mixer_hamiltonian (n_qubits = nqubits )
99
114
qaoa_descriptor = QAOADescriptor (cost_hamil , mixer_hamil , p = p )
100
- variate_params = QAOAVariationalStandardParams (qaoa_descriptor ,
101
- betas , gammas )
102
-
115
+
116
+ # Try to compute the variate params
117
+ _ = QAOAVariationalStandardParams (qaoa_descriptor , betas , gammas )
118
+
103
119
# We mock the potential Exception that could occur in the Device class
104
- azure_device = DeviceAzure ('' , '' , '' )
120
+ azure_device = DeviceAzure ("" , "" , "" )
105
121
azure_device ._check_provider_connection = Mock (return_value = False )
106
-
122
+
107
123
try :
108
- QAOAQiskitQPUBackend (qaoa_descriptor , azure_device ,
109
- shots , None , None , True )
124
+ QAOAQiskitQPUBackend (qaoa_descriptor , azure_device , shots , None , None , True )
110
125
except Exception as e :
111
- self .assertEqual (str (e ), ' Error connecting to AZURE.' )
112
-
126
+ self .assertEqual (str (e ), " Error connecting to AZURE." )
127
+
113
128
with self .assertRaises (Exception ):
114
129
QAOAQiskitQPUBackend (qaoa_descriptor , azure_device , shots , None , None , True )
115
-
116
130
117
- azure_device = DeviceAzure (device_name = '' , resource_id = self .RESOURCE_ID ,
118
- az_location = self .AZ_LOCATION )
119
-
131
+ azure_device = DeviceAzure (
132
+ device_name = "" , resource_id = self .RESOURCE_ID , az_location = self .AZ_LOCATION
133
+ )
134
+
120
135
try :
121
- QAOAQiskitQPUBackend (qaoa_descriptor , azure_device ,
122
- shots , None , None , True )
136
+ QAOAQiskitQPUBackend (qaoa_descriptor , azure_device , shots , None , None , True )
123
137
except Exception as e :
124
- self .assertEqual (str (e ), 'Connection to AZURE was made. Error connecting to the specified backend.' )
125
-
138
+ self .assertEqual (
139
+ str (e ),
140
+ "Connection to AZURE was made. Error connecting to the specified backend." ,
141
+ )
142
+
126
143
with self .assertRaises (Exception ):
127
144
QAOAQiskitQPUBackend (qaoa_descriptor , azure_device , shots , None , None , True )
128
-
145
+
129
146
@pytest .mark .api
130
147
def test_remote_qubit_overflow (self ):
131
-
132
148
"""
133
149
If the user creates a circuit that is larger than the maximum circuit size
134
- that is supported by the QPU. An Exception should be raised with the
150
+ that is supported by the QPU. An Exception should be raised with the
135
151
appropriate error message alerting the user to the error.
136
152
"""
137
-
153
+
138
154
shots = 100
139
-
155
+
140
156
set_of_numbers = np .random .randint (1 , 10 , 6 ).tolist ()
141
157
qubo = NumberPartition (set_of_numbers ).qubo
142
158
143
159
mixer_hamil = X_mixer_hamiltonian (n_qubits = 6 )
144
160
qaoa_descriptor = QAOADescriptor (qubo .hamiltonian , mixer_hamil , p = 1 )
145
- variate_params = create_qaoa_variational_params (qaoa_descriptor , 'standard' , 'rand' )
146
161
147
- azure_device = DeviceAzure ('rigetti.sim.qvm' , self .RESOURCE_ID , self .AZ_LOCATION )
148
-
162
+ # Check the creation of the varitional parms
163
+ _ = create_qaoa_variational_params (qaoa_descriptor , "standard" , "rand" )
164
+
165
+ azure_device = DeviceAzure (
166
+ "rigetti.sim.qvm" , self .RESOURCE_ID , self .AZ_LOCATION
167
+ )
168
+
149
169
try :
150
- QAOAQiskitQPUBackend (qaoa_descriptor , azure_device ,
151
- shots , None , None , True )
170
+ QAOAQiskitQPUBackend (qaoa_descriptor , azure_device , shots , None , None , True )
152
171
except Exception as e :
153
- self .assertEqual (str (e ), 'There are lesser qubits on the device than the number of qubits required for the circuit.' )
172
+ self .assertEqual (
173
+ str (e ),
174
+ "There are lesser qubits on the device than the number of qubits required for the circuit." ,
175
+ )
154
176
155
177
156
178
class TestingQAOAQiskitQPUBackend (unittest .TestCase ):
157
179
158
180
"""This Object tests the QAOA Qiskit QPU Backend objects, which is tasked with the
159
181
creation and execution of a QAOA circuit for the selected QPU provider and
160
182
backend.
161
-
183
+
162
184
IBMQ Account has to be saved locally to run these tests.
163
185
"""
164
186
165
187
@pytest .mark .api
166
188
def setUp (self ):
167
-
189
+ """
190
+ Define the credentials
191
+ """
168
192
self .HUB = "ibm-q"
169
193
self .GROUP = "open"
170
194
self .PROJECT = "main"
@@ -419,7 +443,6 @@ def test_append_circuit(self):
419
443
420
444
@pytest .mark .api
421
445
def test_expectations_in_init (self ):
422
-
423
446
"""
424
447
Testing the Exceptions in the init function of the QiskitQPUShotBasedBackend
425
448
"""
@@ -438,7 +461,9 @@ def test_expectations_in_init(self):
438
461
)
439
462
mixer_hamil = X_mixer_hamiltonian (n_qubits = nqubits )
440
463
qaoa_descriptor = QAOADescriptor (cost_hamil , mixer_hamil , p = p )
441
- variate_params = QAOAVariationalStandardParams (qaoa_descriptor , betas , gammas )
464
+
465
+ # Check the creation of the varitional parms
466
+ _ = QAOAVariationalStandardParams (qaoa_descriptor , betas , gammas )
442
467
443
468
# We mock the potential Exception that could occur in the Device class
444
469
qiskit_device = DeviceQiskit ("" , "" , "" , "" )
@@ -503,7 +528,6 @@ def test_remote_integration_sim_run(self):
503
528
shots = 10000
504
529
505
530
for i in range (4 ):
506
-
507
531
cost_hamil = Hamiltonian (
508
532
[PauliOp ("ZZ" , (0 , 1 )), PauliOp ("ZZ" , (1 , 2 )), PauliOp ("ZZ" , (0 , 2 ))],
509
533
weights ,
@@ -538,7 +562,6 @@ def test_remote_integration_sim_run(self):
538
562
539
563
@pytest .mark .api
540
564
def test_remote_qubit_overflow (self ):
541
-
542
565
"""
543
566
If the user creates a circuit that is larger than the maximum circuit size
544
567
that is supported by the QPU. An Exception should be raised with the
@@ -552,9 +575,9 @@ def test_remote_qubit_overflow(self):
552
575
553
576
mixer_hamil = X_mixer_hamiltonian (n_qubits = 6 )
554
577
qaoa_descriptor = QAOADescriptor (qubo .hamiltonian , mixer_hamil , p = 1 )
555
- variate_params = create_qaoa_variational_params (
556
- qaoa_descriptor , "standard" , "rand"
557
- )
578
+
579
+ # Check the creation of the varitional parms
580
+ _ = create_qaoa_variational_params ( qaoa_descriptor , "standard" , "rand" )
558
581
559
582
qiskit_device = DeviceQiskit ("ibmq_manila" , self .HUB , self .GROUP , self .PROJECT )
560
583
0 commit comments