diff --git a/lib_q_computer_math.py b/lib_q_computer_math.py index 2ea8b79..33c3052 100644 --- a/lib_q_computer_math.py +++ b/lib_q_computer_math.py @@ -141,8 +141,9 @@ class State(Vector): super().__init__(m, *args, **kwargs) self.name = name self.measurement_result = None - if not self._is_normalized(): - raise TypeError("Not a normalized state vector") + # TODO: SHOULD WE NORMALIZE? + # if not self._is_normalized(): + # raise TypeError("Not a normalized state vector") def _is_normalized(self): return np.isclose(np.sum(np.abs(self.m ** 2)), 1.0) @@ -297,11 +298,12 @@ class State(Vector): measurement_result = random.choices(choices_0, weights_0)[0][qubit_n - 1] # TODO: Verify if this is the correct collapse to lower dimension after partial measurement # https://www.youtube.com/watch?v=MG_9JWsrKtM&list=PL1826E60FD05B44E4&index=16 - normalization_factor = np.sqrt(np.sum([self.m[i][0] for i in indexes_for_p_0])) + normalization_factor = np.sqrt(np.sum(np.abs([self.m[i][0] for i in indexes_for_p_0])**2)) # TODO: This can be 0... - self.m = [ - [(self.m[i][0] ** 2) / normalization_factor] for i in indexes_for_p_0 + post_measurement_state = [ + [self.m[i][0] / normalization_factor] for i in indexes_for_p_0 ] + self.m = s('|' + measurement_result + '>') * State(post_measurement_state) return measurement_result def pretty_print(self): @@ -337,16 +339,16 @@ class State(Vector): def test_measure_partial(): - state = s("|010>") - state.measure_partial(2) + state = b_00 + state.measure_partial(1) -def normalize_state(state_vector: ListOrNdarray): +def normalize_state(vector: Vector): """Normalize a state by dividing by the square root of sum of the squares of states""" - norm_coef = np.sqrt(np.sum(np.array(state_vector) ** 2)) + norm_coef = np.sqrt(np.sum(np.array(vector.m) ** 2)) if norm_coef == 0: raise TypeError("zero-sum vector") - return state_vector / norm_coef + return State(vector.m / norm_coef) def s(q, name=None): @@ -530,7 +532,7 @@ class HermitianOperator(LinearTransformation, HermitianMatrix): def operator_func(self, other): """This might not return a normalized state vector so don't wrap it in State""" - return np.dot(self.m, other.m) + return Vector(np.dot(self.m, other.m)) def __repr__(self): if self.name: @@ -1184,6 +1186,7 @@ def test_quantum_processor(): def test_light(): + # http://alienryderflex.com/polarizer/ # TODO: Are these measurement operators the correct way to represent hor/ver/diag filter? # No, becuase they are not unitaries hor_filter = MeasurementOperator.create_from_prob(Matrix(_0.m), name='h') @@ -1191,17 +1194,21 @@ def test_light(): ver_filter = MeasurementOperator.create_from_prob(Matrix(_1.m), name='v') # create random light/photon - random_pol = [[np.random.uniform(0, 1)], [np.random.uniform(0, 1)]] - random_light = State(normalize_state(random_pol)) + random_pol = Vector([[np.random.uniform(0, 1)], [np.random.uniform(0, 1)]]) + random_light = normalize_state(random_pol) + + # TODO: Doesn't work... qc = QuantumCircuit(1, initial_steps=[[random_light]]) + qc.add_row([hor_filter, ver_filter]) + qc.print() + qp = QuantumProcessor(qc) + qp.print_sample(qp.get_sample(100)) # add three filters as operators - # qc.add_row([hor_filter, ver_filter]) + qc = QuantumCircuit(1, initial_steps=[[random_light]]) qc.add_row([hor_filter, diag_filter, ver_filter]) qc.print() qp = QuantumProcessor(qc) - # TODO: This doesn't work - dealing with non-normalized state vectors - # - When measured in horizontal filter, the state vector has a positive |0> and 0 for |1> qp.print_sample(qp.get_sample(100)) diff --git a/play.py b/play.py new file mode 100644 index 0000000..b443510 --- /dev/null +++ b/play.py @@ -0,0 +1,19 @@ +import numpy as np +from lib_q_computer_math import Vector, Matrix, MeasurementOperator, _0, _1, _p, normalize_state + + +def main(): + random_pol = Vector([[np.random.uniform(0, 1)], [np.random.uniform(0, 1)]]) + random_light = normalize_state(random_pol) + hor_filter = MeasurementOperator.create_from_prob(Matrix(_0.m), name='h') + diag_filter = MeasurementOperator.create_from_prob(Matrix(_p.m), name='d') + ver_filter = MeasurementOperator.create_from_prob(Matrix(_1.m), name='v') + print(hor_filter.on(_0)) + print(ver_filter.on(_0)) + print(ver_filter.on(hor_filter.on(_0))) + print(ver_filter.on(diag_filter.on(hor_filter.on(_0)))) + print() + + +if __name__ == "__main__": + main()