-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtest_statevector_estimator.py
312 lines (265 loc) · 12.6 KB
/
test_statevector_estimator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Tests for Estimator."""
import unittest
import numpy as np
from qiskit.circuit import Parameter, QuantumCircuit
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import StatevectorEstimator
from qiskit.primitives.containers.estimator_pub import EstimatorPub
from qiskit.primitives.containers.observables_array import ObservablesArray
from qiskit.primitives.containers.bindings_array import BindingsArray
from qiskit.quantum_info import SparsePauliOp
from test import QiskitTestCase # pylint: disable=wrong-import-order
class TestStatevectorEstimator(QiskitTestCase):
"""Test Estimator"""
def setUp(self):
super().setUp()
self.ansatz = RealAmplitudes(num_qubits=2, reps=2)
self.observable = SparsePauliOp.from_list(
[
("II", -1.052373245772859),
("IZ", 0.39793742484318045),
("ZI", -0.39793742484318045),
("ZZ", -0.01128010425623538),
("XX", 0.18093119978423156),
]
)
self.expvals = -1.0284380963435145, -1.284366511861733
self.psi = (RealAmplitudes(num_qubits=2, reps=2), RealAmplitudes(num_qubits=2, reps=3))
self.params = tuple(psi.parameters for psi in self.psi)
self.hamiltonian = (
SparsePauliOp.from_list([("II", 1), ("IZ", 2), ("XI", 3)]),
SparsePauliOp.from_list([("IZ", 1)]),
SparsePauliOp.from_list([("ZI", 1), ("ZZ", 1)]),
)
self.theta = (
[0, 1, 1, 2, 3, 5],
[0, 1, 1, 2, 3, 5, 8, 13],
[1, 2, 3, 4, 5, 6],
)
def test_estimator_run(self):
"""Test Estimator.run()"""
psi1, psi2 = self.psi
hamiltonian1, hamiltonian2, hamiltonian3 = self.hamiltonian
theta1, theta2, theta3 = self.theta
estimator = StatevectorEstimator()
# Specify the circuit and observable by indices.
# calculate [ <psi1(theta1)|H1|psi1(theta1)> ]
job = estimator.run([(psi1, hamiltonian1, [theta1])])
result = job.result()
np.testing.assert_allclose(result[0].data.evs, [1.5555572817900956])
# Objects can be passed instead of indices.
# Note that passing objects has an overhead
# since the corresponding indices need to be searched.
# User can append a circuit and observable.
# calculate [ <psi2(theta2)|H1|psi2(theta2)> ]
result2 = estimator.run([(psi2, hamiltonian1, theta2)]).result()
np.testing.assert_allclose(result2[0].data.evs, [2.97797666])
# calculate [ <psi1(theta1)|H2|psi1(theta1)>, <psi1(theta1)|H3|psi1(theta1)> ]
result3 = estimator.run([(psi1, [hamiltonian2, hamiltonian3], theta1)]).result()
np.testing.assert_allclose(result3[0].data.evs, [-0.551653, 0.07535239])
# calculate [ [<psi1(theta1)|H1|psi1(theta1)>,
# <psi1(theta3)|H3|psi1(theta3)>],
# [<psi2(theta2)|H2|psi2(theta2)>] ]
result4 = estimator.run(
[(psi1, [hamiltonian1, hamiltonian3], [theta1, theta3]), (psi2, hamiltonian2, theta2)]
).result()
np.testing.assert_allclose(result4[0].data.evs, [1.55555728, -1.08766318])
np.testing.assert_allclose(result4[1].data.evs, [0.17849238])
def test_estimator_with_pub(self):
"""Test estimator with explicit EstimatorPubs."""
psi1, psi2 = self.psi
hamiltonian1, hamiltonian2, hamiltonian3 = self.hamiltonian
theta1, theta2, theta3 = self.theta
obs1 = ObservablesArray.coerce([hamiltonian1, hamiltonian3])
bind1 = BindingsArray.coerce({tuple(psi1.parameters): [theta1, theta3]})
pub1 = EstimatorPub(psi1, obs1, bind1)
obs2 = ObservablesArray.coerce(hamiltonian2)
bind2 = BindingsArray.coerce({tuple(psi2.parameters): theta2})
pub2 = EstimatorPub(psi2, obs2, bind2)
estimator = StatevectorEstimator()
result4 = estimator.run([pub1, pub2]).result()
np.testing.assert_allclose(result4[0].data.evs, [1.55555728, -1.08766318])
np.testing.assert_allclose(result4[1].data.evs, [0.17849238])
def test_estimator_run_no_params(self):
"""test for estimator without parameters"""
circuit = self.ansatz.assign_parameters([0, 1, 1, 2, 3, 5])
est = StatevectorEstimator()
result = est.run([(circuit, self.observable)]).result()
np.testing.assert_allclose(result[0].data.evs, [-1.284366511861733])
def test_run_single_circuit_observable(self):
"""Test for single circuit and single observable case."""
est = StatevectorEstimator()
with self.subTest("No parameter"):
qc = QuantumCircuit(1)
qc.x(0)
op = SparsePauliOp("Z")
param_vals = [None, [], [[]], np.array([]), np.array([[]]), [np.array([])]]
target = [-1]
for val in param_vals:
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["target_precision"], 0)
with self.subTest("One parameter"):
param = Parameter("x")
qc = QuantumCircuit(1)
qc.ry(param, 0)
op = SparsePauliOp("Z")
param_vals = [
[np.pi],
np.array([np.pi]),
]
target = [-1]
for val in param_vals:
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["target_precision"], 0)
with self.subTest("More than one parameter"):
qc = self.psi[0]
op = self.hamiltonian[0]
param_vals = [
self.theta[0],
[self.theta[0]],
np.array(self.theta[0]),
np.array([self.theta[0]]),
[np.array(self.theta[0])],
]
target = [1.5555572817900956]
for val in param_vals:
self.subTest(f"{val}")
result = est.run([(qc, op, val)]).result()
np.testing.assert_allclose(result[0].data.evs, target)
self.assertEqual(result[0].metadata["target_precision"], 0)
def test_run_1qubit(self):
"""Test for 1-qubit cases"""
qc = QuantumCircuit(1)
qc2 = QuantumCircuit(1)
qc2.x(0)
op = SparsePauliOp.from_list([("I", 1)])
op2 = SparsePauliOp.from_list([("Z", 1)])
est = StatevectorEstimator()
result = est.run([(qc, op)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc, op2)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc2, op)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc2, op2)]).result()
np.testing.assert_allclose(result[0].data.evs, [-1])
def test_run_2qubits(self):
"""Test for 2-qubit cases (to check endian)"""
qc = QuantumCircuit(2)
qc2 = QuantumCircuit(2)
qc2.x(0)
op = SparsePauliOp.from_list([("II", 1)])
op2 = SparsePauliOp.from_list([("ZI", 1)])
op3 = SparsePauliOp.from_list([("IZ", 1)])
est = StatevectorEstimator()
result = est.run([(qc, op)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc2, op)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc, op2)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc2, op2)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc, op3)]).result()
np.testing.assert_allclose(result[0].data.evs, [1])
result = est.run([(qc2, op3)]).result()
np.testing.assert_allclose(result[0].data.evs, [-1])
def test_run_errors(self):
"""Test for errors"""
qc = QuantumCircuit(1)
qc2 = QuantumCircuit(2)
op = SparsePauliOp.from_list([("I", 1)])
op2 = SparsePauliOp.from_list([("II", 1)])
est = StatevectorEstimator()
with self.assertRaises(ValueError):
est.run([(qc, op2)]).result()
with self.assertRaises(ValueError):
est.run([(qc, op, [[1e4]])]).result()
with self.assertRaises(ValueError):
est.run([(qc2, op2, [[1, 2]])]).result()
with self.assertRaises(ValueError):
est.run([(qc, [op, op2], [[1]])]).result()
with self.assertRaises(ValueError):
est.run([(qc, op)], precision=-1).result()
with self.assertRaises(ValueError):
est.run([(qc, 1j * op)], precision=0.1).result()
with self.subTest("missing []"):
with self.assertRaisesRegex(ValueError, "An invalid Estimator pub-like was given"):
_ = est.run((qc, op)).result()
def test_run_numpy_params(self):
"""Test for numpy array as parameter values"""
qc = RealAmplitudes(num_qubits=2, reps=2)
op = SparsePauliOp.from_list([("IZ", 1), ("XI", 2), ("ZY", -1)])
k = 5
rng = np.random.default_rng(12)
params_array = rng.random((k, qc.num_parameters))
params_list = params_array.tolist()
params_list_array = list(params_array)
estimator = StatevectorEstimator()
target = estimator.run([(qc, op, params_list)]).result()
with self.subTest("ndarrary"):
result = estimator.run([(qc, op, params_array)]).result()
self.assertEqual(len(result[0].data.evs), k)
np.testing.assert_allclose(result[0].data.evs, target[0].data.evs)
with self.subTest("list of ndarray"):
result = estimator.run([(qc, op, params_list_array)]).result()
self.assertEqual(len(result[0].data.evs), k)
np.testing.assert_allclose(result[0].data.evs, target[0].data.evs)
def test_precision_seed(self):
"""Test for precision and seed"""
estimator = StatevectorEstimator(default_precision=1.0, seed=1)
psi1 = self.psi[0]
hamiltonian1 = self.hamiltonian[0]
theta1 = self.theta[0]
job = estimator.run([(psi1, hamiltonian1, [theta1])])
result = job.result()
np.testing.assert_allclose(result[0].data.evs, [1.901141473854881])
# The result of the second run is the same
job = estimator.run([(psi1, hamiltonian1, [theta1]), (psi1, hamiltonian1, [theta1])])
result = job.result()
np.testing.assert_allclose(result[0].data.evs, [1.901141473854881])
np.testing.assert_allclose(result[1].data.evs, [1.901141473854881])
# precision=0 implies the exact expectation value
job = estimator.run([(psi1, hamiltonian1, [theta1])], precision=0)
result = job.result()
np.testing.assert_allclose(result[0].data.evs, [1.5555572817900956])
def test_iter_pub(self):
"""test for an iterable of pubs"""
estimator = StatevectorEstimator()
circuit = self.ansatz.assign_parameters([0, 1, 1, 2, 3, 5])
observable = self.observable.apply_layout(circuit.layout)
result = estimator.run(iter([(circuit, observable), (circuit, observable)])).result()
np.testing.assert_allclose(result[0].data.evs, [-1.284366511861733])
np.testing.assert_allclose(result[1].data.evs, [-1.284366511861733])
def test_metadata(self):
"""Test for metadata"""
qc = QuantumCircuit(2)
qc2 = QuantumCircuit(2)
qc2.metadata = {"a": 1}
estimator = StatevectorEstimator()
result = estimator.run([(qc, "ZZ"), (qc2, "ZZ")], precision=0.1).result()
self.assertEqual(len(result), 2)
self.assertEqual(result.metadata, {"version": 2})
self.assertEqual(
result[0].metadata, {"target_precision": 0.1, "circuit_metadata": qc.metadata}
)
self.assertEqual(
result[1].metadata, {"target_precision": 0.1, "circuit_metadata": qc2.metadata}
)
if __name__ == "__main__":
unittest.main()