-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost_functions.py
82 lines (50 loc) · 1.93 KB
/
cost_functions.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
import pennylane as qml
import pennylane.numpy as np
def fidelity(state0, state1, dt):
return (1 - qml.math.fidelity(state0, state1)) / dt ** 2
### In development below
def projector_zero(n_qubits):
zero_state = np.zeros(2 ** n_qubits)
zero_state[0] = 1
return np.outer(zero_state, zero_state)
def jth_projector(n_qubits, j):
jth_state = np.zeros(n_qubits)
jth_state[j] = 1
eye_bar = np.eye(n_qubits)
eye_bar[j, j] = 0
return np.tensordot(np.outer(jth_state, jth_state), eye_bar, axis=0)
def local_fidelity(state0, state1):
n_qubits = 3 # how can we get this value from the state0 object?
for j in range(n_qubits):
jth_state = np.zeros(n_qubits)
jth_state[j] = 1
eye_bar = np.eye(n_qubits)
eye_bar[j, j] = 0
jth_prod += np.tensordot(np.outer(jth_state, jth_state) / n_qubits, eye_bar, axis=0)
np.eye(n_qubits) - np.tensordot(np.outer(jth_state, jth_state) / n_qubits, eye_bar, axis=0)
def global_op(n_qubits):
return np.eye(2 ** n_qubits) - projector_zero(n_qubits)
### original qiskit implementation
def projector__zero(n_qubits):
# This function create the global projector |00...0><00...0|
from qiskit.opflow import Z, I
prj_list = [0.5 * (I + Z) for i in range(n_qubits)]
prj = prj_list[0]
for a in range(1, len(prj_list)):
prj = prj ^ prj_list[a]
return prj
def projector_zero_local(n_qubits):
# This function creates the local version of the cost function
# proposed by Cerezo et al: https://www.nature.com/articles/s41467-021-21728-w
from qiskit.opflow import Z, I
tot_prj = 0
for k in range(n_qubits):
prj_list = [I for i in range(n_qubits)]
prj_list[k] = 0.5 * (I + Z)
prj = prj_list[0]
for a in range(1, len(prj_list)):
prj = prj ^ prj_list[a]
# print(prj)
tot_prj += prj
tot_prj = (1 / n_qubits) * tot_prj
return tot_prj