From b80fa8e3e048acb3ebf937413c7410347e75bdb3 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Wed, 4 Dec 2019 10:08:37 +0100 Subject: [PATCH] initial interface for libv --- lib_q_computer.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib_q_computer.py diff --git a/lib_q_computer.py b/lib_q_computer.py new file mode 100644 index 0000000..9dcf789 --- /dev/null +++ b/lib_q_computer.py @@ -0,0 +1,44 @@ +import numpy as np + + +class State(object): + def __init__(self, matrix_state): + self._matrix_state = matrix_state + + def __repr__(self): + return str(self._matrix_state) + + +class Gate(State): + def on(self, state, q2=None): + this = self._matrix_state + a = state._matrix_state + if q2: + a = np.kron(a, q2._matrix_state) + return State(this.dot(a)) + + +class Qubit(State): ... + + +_0 = np.array([[1], + [0]]) + +H = Gate(np.array([ + [1 / np.sqrt(2), 1 / np.sqrt(2)], + [1 / np.sqrt(2), -1 / np.sqrt(2)] +])) + +CNOT = Gate(np.array([ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0], +])) + +if __name__ == "__main__": + # E.g. Bell state + q1 = Qubit(_0) + q2 = Qubit(_0) + bell = CNOT.on(H.on(q1), q2) + print(bell)