-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
59 lines (42 loc) · 1.62 KB
/
example.py
File metadata and controls
59 lines (42 loc) · 1.62 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
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy
import time
import qiskit
from qiskit import QuantumRegister, QuantumCircuit, ClassicalRegister, BasicAer
from qiskit.quantum_info import state_fidelity
import qiskit.ignis.verification.tomography as tomo
from bme_fit import *
#number of trials
SHOTS = 10
#number of qubits
n = 2
#create a qubit register
q_reg = QuantumRegister(n)
c_reg = ClassicalRegister(n)
#create a quantum circuit
circuit = QuantumCircuit(q_reg)
#append gates to the quantum circuit
circuit.h(q_reg[0])
print(circuit.qasm())
#run the circuit
job = qiskit.execute(circuit, BasicAer.get_backend('statevector_simulator'))
#get wave function at the end of the circuit
psi = job.result().get_statevector(circuit)
print('final state of the circuit',psi)
# Generate circuits for tomography and run tomography on simulator
t = time.time()
qst = tomo.state_tomography_circuits(circuit, q_reg)
job = qiskit.execute(qst, BasicAer.get_backend('qasm_simulator'), shots=SHOTS)
print('Time taken:', time.time() - t)
tomo_fitter = tomo.fitters.base_fitter.TomographyFitter(job.result(), qst)
# Extract tomography data so that counts are indexed by measurement configuration
#tomo_counts = tomo.tomography_data(job.result(), qst)
# Generate fitter data and reconstruct density matrix
probs, basis_matrix, weights = tomo_fitter._fitter_data(True, 0.5)
#print('tomography results', tomo_counts)
#bayesian reconstruction of the density matrix and error on observable
rho_fit, error_rho = bme_fit(probs, basis_matrix, SHOTS)
print("BME", rho_fit)
#maximum likelihood reconstruction of density matrix
rho_mle = tomo_fitter.fit()
print("MLE:", rho_mle)
print("error", error_rho)