redo inner

This commit is contained in:
Daniel Tsvetkov 2020-03-27 14:36:28 +01:00
parent 4fd95fcbf6
commit 06ac6dc6e8
1 changed files with 20 additions and 21 deletions

41
lib.py
View File

@ -65,7 +65,7 @@ class Matrix(object):
if isinstance(other, Complex): if isinstance(other, Complex):
return Matrix(self.m * other) return Matrix(self.m * other)
elif isinstance(other, Matrix): elif isinstance(other, Matrix):
return self.__class__(np.kron(self.m, other.m)) return Matrix(np.kron(self.m, other.m))
raise NotImplementedError raise NotImplementedError
def __rmul__(self, other): def __rmul__(self, other):
@ -74,19 +74,6 @@ class Matrix(object):
def __mul__(self, other): def __mul__(self, other):
return self.__mul_or_kron__(other) return self.__mul_or_kron__(other)
def __or__(self, other):
"""Define inner product: <self|other>
"""
m = np.dot(self._conjugate_transpose(), other.m)
try:
return self.__class__(m)
except:
try:
return other.__class__(m)
except:
...
return Matrix(m)
def __len__(self): def __len__(self):
return len(self.m) return len(self.m)
@ -120,6 +107,10 @@ class Matrix(object):
"""Define outer product |0><0| looks like |0x0| which is 0.x(0)""" """Define outer product |0><0| looks like |0x0| which is 0.x(0)"""
return self.outer(other) return self.outer(other)
def kron(self, other):
"""Define kronicker product - |0> ⊗ |0> = |00>"""
return Matrix(np.kron(self.m, other.m))
def _conjugate_transpose(self): def _conjugate_transpose(self):
return self.m.transpose().conjugate() return self.m.transpose().conjugate()
@ -267,17 +258,17 @@ class State(Vector):
def length(self): def length(self):
"""Norm/Length of the vector = sqrt(<self|self>)""" """Norm/Length of the vector = sqrt(<self|self>)"""
return np.sqrt((self | self).m).item(0) return np.sqrt((self.inner(self)).m).item(0)
def is_orthogonal(self, other): def is_orthogonal(self, other):
"""If the inner (dot) product is zero, this vector is orthogonal to other""" """If the inner (dot) product is zero, this vector is orthogonal to other"""
return self | other == 0 return self.inner(other) == 0
def get_prob(self, j): def get_prob(self, j):
"""pr(j) = |<e_j|self>|^2""" """pr(j) = |<e_j|self>|^2"""
# j_th basis vector # j_th basis vector
e_j = State([[1] if i == int(j) else [0] for i in range(len(self.m))]) e_j = State([[1] if i == int(j) else [0] for i in range(len(self.m))])
return np.absolute((e_j | self).m.item(0)) ** 2 return np.absolute((e_j.inner(self)).m.item(0)) ** 2
def get_fmt_of_element(self): def get_fmt_of_element(self):
return "{:0" + str(int(np.ceil(np.log2(len(self))))) + "b}" return "{:0" + str(int(np.ceil(np.log2(len(self))))) + "b}"
@ -830,9 +821,11 @@ class MeasurementOperator(HermitianOperator):
return state_ct.m.dot(self.m.dot(state.m)).item() return state_ct.m.dot(self.m.dot(state.m)).item()
m0 = MeasurementOperator.create_from_prob(_0)
m1 = MeasurementOperator.create_from_prob(_1)
def test_measurement_ops(): def test_measurement_ops():
m0 = MeasurementOperator.create_from_prob(Matrix([1, 0]))
m1 = MeasurementOperator.create_from_prob(Matrix([0, 1]))
assert m0 == Matrix([[1, 0], assert m0 == Matrix([[1, 0],
[0, 0]]) [0, 0]])
assert m1 == Matrix([[0, 0], assert m1 == Matrix([[0, 0],
@ -1113,7 +1106,7 @@ class QuantumCircuit(object):
if not self.current_quantum_state: if not self.current_quantum_state:
self.current_quantum_state = step_quantum_state self.current_quantum_state = step_quantum_state
else: else:
self.current_quantum_state = step_quantum_state | self.current_quantum_state self.current_quantum_state = step_quantum_state.inner(self.current_quantum_state)
self.current_step += 1 self.current_step += 1
def run(self): def run(self):
@ -1172,7 +1165,7 @@ class QuantumProcessor(object):
if not self.c_q_state: if not self.c_q_state:
self.c_q_state = step_quantum_state self.c_q_state = step_quantum_state
else: else:
self.c_q_state = State((step_quantum_state | self.c_q_state).m) self.c_q_state = State((step_quantum_state.inner(self.c_q_state)).m)
self.c_step += 1 self.c_step += 1
def get_next_step(self): def get_next_step(self):
@ -1270,4 +1263,10 @@ if __name__ == "__main__":
test() test()
test_quantum_processor() test_quantum_processor()
test_light() test_light()
# Partial measurement - I want to measure just the first qubit...
# TODO - makes sense???
# m0_ = m0 * I
# post_measurement = b_phi_m.measure_with_op(m0_)
# result_0 = post_measurement.measure()
# test_measure_partial() # test_measure_partial()