-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.py
96 lines (73 loc) · 3.16 KB
/
Example.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
# %%
from variational_circuit.vcirc import *
from variational_circuit.measure import *
from variational_circuit.optimize import *
from time import time
from qutip.random_objects import rand_ket
from qutip.qip.qubits import qubit_states
from qutip.tensor import tensor
# %% [markdown]
# Three samples that reduce the dimension of a quantum state are given
# %% Generate random input
N = 4 # number of qubits
n = 2 # number of reduced qubits
# Random inputs (qubit state)
Input = rand_ket(2**N,dims=[[2]*N,[1]*N])
# Input2 = rand_ket(2**N,dims=[[2]*N,[1]*N])
# Input = 0.99*Input*Input.dag() + 0.01*Input2*Input2.dag() # To add some inpurity
# Reference State
r_state = qubit_states(n) # 0 state
# %% Reduce the dimension of a quantum state by bring one of the subsystem to 0 state
print("quantum dimensionality reduction")
vc1 = vcirc(N)
for L in np.arange(1,5): # number of ansatz
vc1.add_ansatz(np.zeros(N*3)) # add one layer
t0 = time()
x0 = np.zeros(N*3*L) # init parameters
res = circ_maximize(x0,Input,vc1,fid_ref,r_state,[2,3],opt_method='powell')
print("L=",L,"Result=",res.fun,"Iterated",res.nit,"tiems; running time=",time()-t0,"seconds")
if 1-res.fun < 1e-5:
break
out = vc1.apply_to(Input,update=True)
print("result fidelity is",fid_ref(tensor(out.ptrace([0,1]),r_state*r_state.dag()),out))
# %% Reduce the dimension of a quantum state by first disentangle the qubits, and then bring one of the subsystem to 0 state.
print("disentangling")
vc2 = vcirc(N)
for L in np.arange(1,5): # number of ansatz
vc2.add_ansatz(np.zeros(N*3)) # add one layer
t0 = time()
x0 = np.zeros(N*3*L) # init parameters
res = circ_maximize(x0,Input,vc1,sep_purity,[[0,1],[2,3]],opt_method='powell')
print("L=",L,"Result=",res.fun,"Iterated",res.nit,"tiems; running time=",time()-t0,"seconds")
if 1-res.fun < 1e-5:
break
print("The second phase")
vc21 = vcirc(n)
out = vc2.apply_to(Input,update=True)
state2 = out.ptrace([2,3])
for L in np.arange(1,5):
vc21.add_ansatz(np.zeros(n*3)) # add one layer
t0 = time()
x0 = np.zeros(n*3*L) # init parameters
res = circ_maximize(x0,state2,vc21,fid_ref,r_state,opt_method='powell')
print("L=",L,"Result=",res.fun,"Iterated",res.nit,"tiems; running time=",time()-t0,"seconds")
if 1-res.fun < 1e-5:
break
out2 = vc21.apply_to(state2,update=True)
final = tensor(out.ptrace([0,1]),out2)
print("result fidelity is",fid_ref(final,tensor(out.ptrace([0,1]),r_state*r_state.dag())))
# %% Reduce the dimension of a quantum state by minimize the entropy of one of the state.
print("dimensionality reduction with arbitrary classical memory")
vc3 = vcirc(N)
for L in np.arange(1,5): # number of ansatz
vc3.add_ansatz(np.zeros(N*3)) # add one layer
t0 = time()
x0 = np.zeros(N*3*L) # init parameters
res = circ_minimize(x0,Input,vc3,c_entropy,[2,3],opt_method='powell')
print("L=",L,"Result=",res.fun,"Iterated",res.nit,"tiems; running time=",time()-t0,"seconds")
if res.fun < 1e-5:
break
out = vc3.apply_to(Input)
conv_ref = out.ptrace([2,3])
print("The ref state is",conv_ref)
print("result fidelity is",fid_ref(tensor(out.ptrace([0,1]),conv_ref),out))