-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathState.py
More file actions
72 lines (65 loc) · 3.2 KB
/
State.py
File metadata and controls
72 lines (65 loc) · 3.2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
import numpy as np
class State:
def __init__(self, dim=None, state=None, densityMatrix=None):
if(dim is None):
raise ValueError('dim of state not defined')
if(type(dim)!=int):
raise ValueError('dim should be int')
self.dim=dim
if(densityMatrix is None):
if(state is None):
self.state=1
for i in range(dim):
self.state=np.kron(self.state, [1,0])
self.densityMatrix = np.dot(state, state.conjugate().reshape(1, 2 ** dim))
else:
if(type(state)==list):
state=np.array(state)
if(state.shape!=(2**dim, ) and state.shape!=(2**dim, 1)):
raise ValueError('state shape should be %s' % (str((2**dim,))))
self.state=state.reshape(2**dim,1)
self.densityMatrix=np.dot(state.reshape(2**dim,1), state.conjugate().reshape(1,2**dim))
elif (type(state)==np.ndarray):
if(state.shape!=(2**dim,) and state.shape!=(2**dim,1)):
raise ValueError('state shape should be %s or %s' % (str((2 ** dim,)), str((2**dim,1))))
self.state=state.reshape(2**dim,1)
self.densityMatrix=np.dot(state.reshape(2**dim,1), state.conjugate().reshape(1,2**dim))
else:
raise ValueError('state type error')
else:
if (type(densityMatrix) == list):
densityMatrix = np.array(densityMatrix)
if (densityMatrix.shape != (2 ** dim, 2 ** dim)):
raise ValueError('density matrix shape should be %s' % (str((2 ** dim, 2 ** dim))))
self.densityMatrix = densityMatrix
elif (type(densityMatrix) == np.ndarray):
if (densityMatrix.shape != (2 ** dim, 2 ** dim)):
raise ValueError('density matrix shape should be %s' % (str((2 ** dim, 2 ** dim))))
self.densityMatrix= densityMatrix
else:
raise ValueError('density matrix type error')
def randomState(dim=1):#old fashioned, need updating
pa = np.random.rand(2)
pa /= np.sqrt(np.sum(np.square(np.abs(pa))))
pa = np.array([pa[0], pa[1]*np.exp(1j*np.random.random())]).reshape(2, 1)
res=pa
for i in range(dim-1):
pa = np.random.rand(2)
pa /= np.sqrt(np.sum(np.square(np.abs(pa))))
pa = np.array([pa[0], pa[1]*np.exp(1j*np.random.random())]).reshape(2, 1)
res=np.kron(res,pa)
return res
def randomStateClass(dim=1):#return a class <State> variable
pa = np.random.rand(2)
pa /= np.sqrt(np.sum(np.square(np.abs(pa))))
pa = np.array([pa[0], pa[1]*np.exp(1j*np.random.random())]).reshape(2, 1)
res=pa
for i in range(dim-1):
pa = np.random.rand(2)
pa /= np.sqrt(np.sum(np.square(np.abs(pa))))
pa = np.array([pa[0], pa[1]*np.exp(1j*np.random.random())]).reshape(2, 1)
res=np.kron(res,pa)
resState=State(dim, res)
return resState
def catState(dim=1):#return a cat state in <Class 'State'> form
pa = np.zeros(2**dim, 1)