slight fixes
This commit is contained in:
parent
beffc3ab2c
commit
26bc532a81
@ -141,8 +141,9 @@ class State(Vector):
|
|||||||
super().__init__(m, *args, **kwargs)
|
super().__init__(m, *args, **kwargs)
|
||||||
self.name = name
|
self.name = name
|
||||||
self.measurement_result = None
|
self.measurement_result = None
|
||||||
if not self._is_normalized():
|
# TODO: SHOULD WE NORMALIZE?
|
||||||
raise TypeError("Not a normalized state vector")
|
# if not self._is_normalized():
|
||||||
|
# raise TypeError("Not a normalized state vector")
|
||||||
|
|
||||||
def _is_normalized(self):
|
def _is_normalized(self):
|
||||||
return np.isclose(np.sum(np.abs(self.m ** 2)), 1.0)
|
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]
|
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
|
# 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
|
# 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...
|
# TODO: This can be 0...
|
||||||
self.m = [
|
post_measurement_state = [
|
||||||
[(self.m[i][0] ** 2) / normalization_factor] for i in indexes_for_p_0
|
[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
|
return measurement_result
|
||||||
|
|
||||||
def pretty_print(self):
|
def pretty_print(self):
|
||||||
@ -337,16 +339,16 @@ class State(Vector):
|
|||||||
|
|
||||||
|
|
||||||
def test_measure_partial():
|
def test_measure_partial():
|
||||||
state = s("|010>")
|
state = b_00
|
||||||
state.measure_partial(2)
|
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"""
|
"""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:
|
if norm_coef == 0:
|
||||||
raise TypeError("zero-sum vector")
|
raise TypeError("zero-sum vector")
|
||||||
return state_vector / norm_coef
|
return State(vector.m / norm_coef)
|
||||||
|
|
||||||
|
|
||||||
def s(q, name=None):
|
def s(q, name=None):
|
||||||
@ -530,7 +532,7 @@ class HermitianOperator(LinearTransformation, HermitianMatrix):
|
|||||||
|
|
||||||
def operator_func(self, other):
|
def operator_func(self, other):
|
||||||
"""This might not return a normalized state vector so don't wrap it in State"""
|
"""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):
|
def __repr__(self):
|
||||||
if self.name:
|
if self.name:
|
||||||
@ -1184,6 +1186,7 @@ def test_quantum_processor():
|
|||||||
|
|
||||||
|
|
||||||
def test_light():
|
def test_light():
|
||||||
|
# http://alienryderflex.com/polarizer/
|
||||||
# TODO: Are these measurement operators the correct way to represent hor/ver/diag filter?
|
# TODO: Are these measurement operators the correct way to represent hor/ver/diag filter?
|
||||||
# No, becuase they are not unitaries
|
# No, becuase they are not unitaries
|
||||||
hor_filter = MeasurementOperator.create_from_prob(Matrix(_0.m), name='h')
|
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')
|
ver_filter = MeasurementOperator.create_from_prob(Matrix(_1.m), name='v')
|
||||||
|
|
||||||
# create random light/photon
|
# create random light/photon
|
||||||
random_pol = [[np.random.uniform(0, 1)], [np.random.uniform(0, 1)]]
|
random_pol = Vector([[np.random.uniform(0, 1)], [np.random.uniform(0, 1)]])
|
||||||
random_light = State(normalize_state(random_pol))
|
random_light = normalize_state(random_pol)
|
||||||
|
|
||||||
|
# TODO: Doesn't work...
|
||||||
qc = QuantumCircuit(1, initial_steps=[[random_light]])
|
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
|
# 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.add_row([hor_filter, diag_filter, ver_filter])
|
||||||
qc.print()
|
qc.print()
|
||||||
qp = QuantumProcessor(qc)
|
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))
|
qp.print_sample(qp.get_sample(100))
|
||||||
|
|
||||||
|
|
||||||
|
19
play.py
Normal file
19
play.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user