-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2_util.py
81 lines (66 loc) · 2.66 KB
/
lab2_util.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
import numpy as np
from qiskit import transpile, QuantumCircuit
def version_check():
import qiskit
if qiskit.version.VERSION == '1.0.2':
return print("You have the right version! Enjoy the challenge!")
else:
return print("please install right version by copy/paste and execute - !pip install 'qiskit[visualization]' == 1.0.2'")
def transpile_scoring(circ, layout, backend):
"""
A custom cost function that includes T1 and T2 computed during idle periods
Parameters:
circ (QuantumCircuit): circuit of interest
layouts (list of lists): List of specified layouts
backend (IBMQBackend): An IBM Quantum backend instance
Returns:
float: Fidelity of circ
"""
fid = 1
touched = set()
dt = backend.dt
num_qubits = backend.num_qubits
error=0
t1s = [backend.qubit_properties(qq).t1 for qq in range(num_qubits)]
t2s = [backend.qubit_properties(qq).t2 for qq in range(num_qubits)]
for item in circ._data:
for gate in backend.operation_names:
if item[0].name == gate:
if (item[0].name == 'cz') or (item[0].name == 'ecr'):
q0 = circ.find_bit(item[1][0]).index
q1 = circ.find_bit(item[1][1]).index
fid *= 1 - backend.target[item[0].name][(q0, q1)].error
touched.add(q0)
touched.add(q1)
elif item[0].name == 'measure':
q0 = circ.find_bit(item[1][0]).index
fid *= 1 - backend.target[item[0].name][(q0, )].error
touched.add(q0)
elif item[0].name == 'delay':
q0 = circ.find_bit(item[1][0]).index
# Ignore delays that occur before gates
# This assumes you are in ground state and errors
# do not occur.
if q0 in touched:
time = item[0].duration * dt
fid *= 1-qubit_error(time, t1s[q0], t2s[q0])
else:
q0 = circ.find_bit(item[1][0]).index
fid *= 1 - backend.target[item[0].name][(q0, )].error
touched.add(q0)
return fid
def qubit_error(time, t1, t2):
"""Compute the approx. idle error from T1 and T2
Parameters:
time (float): Delay time in sec
t1 (float): T1 time in sec
t2 (float): T2 time in sec
Returns:
float: Idle error
"""
t2 = min(t1, t2)
rate1 = 1/t1
rate2 = 1/t2
p_reset = 1-np.exp(-time*rate1)
p_z = (1-p_reset)*(1-np.exp(-time*(rate2-rate1)))/2
return p_z + p_reset