1
1
# This code is part of a Qiskit project.
2
2
#
3
- # (C) Copyright IBM 2022, 2023 .
3
+ # (C) Copyright IBM 2022, 2024 .
4
4
#
5
5
# This code is licensed under the Apache License, Version 2.0. You may
6
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
22
22
from qiskit .circuit .library import TwoLocal , RealAmplitudes
23
23
from qiskit .primitives import Sampler , Estimator
24
24
from qiskit .quantum_info import SparsePauliOp
25
- from qiskit .quantum_info .operators import Operator
26
25
27
26
from qiskit_algorithms .eigensolvers import VQD , VQDResult
28
27
from qiskit_algorithms import AlgorithmError
39
38
("XX" , 0.18093119978423156 ),
40
39
]
41
40
)
42
- H2_OP = Operator (H2_SPARSE_PAULI .to_matrix ())
43
41
44
42
45
43
@ddt
@@ -64,7 +62,7 @@ def setUp(self):
64
62
self .fidelity = ComputeUncompute (Sampler ())
65
63
self .betas = [50 , 50 ]
66
64
67
- @data (H2_OP , H2_SPARSE_PAULI )
65
+ @data (H2_SPARSE_PAULI )
68
66
def test_basic_operator (self , op ):
69
67
"""Test the VQD without aux_operators."""
70
68
wavefunction = self .ryrz_wavefunction
@@ -127,7 +125,7 @@ def test_beta_autoeval(self, op):
127
125
beta = float (logs .output [0 ].split ()[- 1 ])
128
126
self .assertAlmostEqual (beta , 20.40459399499687 , 4 )
129
127
130
- @data (H2_OP , H2_SPARSE_PAULI )
128
+ @data (H2_SPARSE_PAULI )
131
129
def test_mismatching_num_qubits (self , op ):
132
130
"""Ensuring circuit and operator mismatch is caught"""
133
131
wavefunction = QuantumCircuit (1 )
@@ -143,7 +141,7 @@ def test_mismatching_num_qubits(self, op):
143
141
with self .assertRaises (AlgorithmError ):
144
142
_ = vqd .compute_eigenvalues (operator = op )
145
143
146
- @data (H2_OP , H2_SPARSE_PAULI )
144
+ @data (H2_SPARSE_PAULI )
147
145
def test_missing_varform_params (self , op ):
148
146
"""Test specifying a variational form with no parameters raises an error."""
149
147
circuit = QuantumCircuit (op .num_qubits )
@@ -158,7 +156,7 @@ def test_missing_varform_params(self, op):
158
156
with self .assertRaises (AlgorithmError ):
159
157
vqd .compute_eigenvalues (operator = op )
160
158
161
- @data (H2_OP , H2_SPARSE_PAULI )
159
+ @data (H2_SPARSE_PAULI )
162
160
def test_callback (self , op ):
163
161
"""Test the callback on VQD."""
164
162
history = {"eval_count" : [], "parameters" : [], "mean" : [], "metadata" : [], "step" : []}
@@ -202,7 +200,7 @@ def store_intermediate_result(eval_count, parameters, mean, metadata, step):
202
200
np .testing .assert_array_almost_equal (history ["mean" ], ref_mean , decimal = 2 )
203
201
np .testing .assert_array_almost_equal (history ["step" ], ref_step , decimal = 0 )
204
202
205
- @data (H2_OP , H2_SPARSE_PAULI )
203
+ @data (H2_SPARSE_PAULI )
206
204
def test_vqd_optimizer (self , op ):
207
205
"""Test running same VQD twice to re-use optimizer, then switch optimizer"""
208
206
@@ -241,7 +239,7 @@ def run_check():
241
239
result = vqd .compute_eigenvalues (operator = op )
242
240
self .assertIsInstance (result , VQDResult )
243
241
244
- @data (H2_OP , H2_SPARSE_PAULI )
242
+ @data (H2_SPARSE_PAULI )
245
243
def test_optimizer_list (self , op ):
246
244
"""Test sending an optimizer list"""
247
245
@@ -281,7 +279,7 @@ def test_optimizer_list(self, op):
281
279
result .eigenvalues .real , self .h2_energy_excited [:2 ], decimal = 3
282
280
)
283
281
284
- @data (H2_OP , H2_SPARSE_PAULI )
282
+ @data (H2_SPARSE_PAULI )
285
283
def test_aux_operators_list (self , op ):
286
284
"""Test list-based aux_operators."""
287
285
wavefunction = self .ry_wavefunction
@@ -334,7 +332,7 @@ def test_aux_operators_list(self, op):
334
332
self .assertIsInstance (result .aux_operators_evaluated [0 ][1 ][1 ], dict )
335
333
self .assertIsInstance (result .aux_operators_evaluated [0 ][3 ][1 ], dict )
336
334
337
- @data (H2_OP , H2_SPARSE_PAULI )
335
+ @data (H2_SPARSE_PAULI )
338
336
def test_aux_operators_dict (self , op ):
339
337
"""Test dictionary compatibility of aux_operators"""
340
338
wavefunction = self .ry_wavefunction
@@ -388,7 +386,7 @@ def test_aux_operators_dict(self, op):
388
386
self .assertIsInstance (result .aux_operators_evaluated [0 ]["aux_op2" ][1 ], dict )
389
387
self .assertIsInstance (result .aux_operators_evaluated [0 ]["zero_operator" ][1 ], dict )
390
388
391
- @data (H2_OP , H2_SPARSE_PAULI )
389
+ @data (H2_SPARSE_PAULI )
392
390
def test_aux_operator_std_dev (self , op ):
393
391
"""Test non-zero standard deviations of aux operators."""
394
392
wavefunction = self .ry_wavefunction
0 commit comments