forked from qiskit-community/quantum-prototype-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tomography.py
More file actions
46 lines (38 loc) · 1.72 KB
/
Copy pathtest_tomography.py
File metadata and controls
46 lines (38 loc) · 1.72 KB
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
from unittest import TestCase
import numpy as np
from qiskit_aer import Aer
from qiskit.circuit.library import RealAmplitudes
from qiskit.primitives import Sampler
from qiskit_tomography_toolbox import FullQST, SimulatorQST, HTreeQST, ShadowQST
class TestTomography(TestCase):
"""Test the tomography methods"""
def setUp(self):
"""Set up the test"""
super().setUp()
# define ansatz
num_qubits = 2
self.ansatz = RealAmplitudes(num_qubits=num_qubits, reps=3, entanglement="full")
self.parameters = 2 * np.pi * np.random.rand(self.ansatz.num_parameters)
self.ref = SimulatorQST(self.ansatz).get_relative_amplitude_sign(
self.parameters
)
def test_full_qst(self):
"""Test the full qst from qiskit exp."""
backend = Aer.get_backend("statevector_simulator")
_ = FullQST(self.ansatz, backend, shots=10000)
# this test fails on GH actions but not locally ...
# sol = full_qst.get_relative_amplitude_sign(self.parameters)
# assert np.allclose(self.ref, sol) or np.allclose(self.ref, -sol)
def test_htree_qst(self):
"""Test the htree tomography"""
sampler = Sampler()
htree_qst = HTreeQST(self.ansatz, sampler)
sol = htree_qst.get_relative_amplitude_sign(self.parameters)
assert np.allclose(self.ref, sol) or np.allclose(self.ref, -sol)
def test_shadow_qst(self):
"""Test the classical shadow qst."""
sampler = Sampler()
shadow_qst = ShadowQST(self.ansatz, sampler, 10000)
_ = shadow_qst.get_relative_amplitude_sign(self.parameters)
# this test can also fail on GH
# assert np.allclose(self.ref, sol) or np.allclose(self.ref, -sol)