# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

import numpy as np;
import math;
import random;


# +
class Qubit():
    def __init__(self, initialState = np.array([1,0], dtype=complex)):
        if (self.checkState(initialState)):
            self.initialState = initialState;
            self.currentState = initialState;
        else:
            raise Exception("Does not meet normalization constraint.");
    
    def getInitialState(self):
        return self.initialState;
    
    def checkState(self, state):
        if (np.linalg.norm(state) == 1):
            return True;
        else:
            return False;
        
    def getCurrentState(self):
        return self.currentState;
    
    def setCurrentState(self, state):
        self.currentState = state;
        
    def measure(self, iterations = 100):
        prob0 = abs(self.currentState[0] ** 2);
        prob1 = abs(self.currentState[1] ** 2);
        print(prob1);
        distribution = [0, 0];
        for i in range(iterations):
            if (random.uniform(0, 1) > prob1):
                distribution[0] += 1;
            else:
                distribution[1] += 1;
                
        print(distribution);
        
class Gate():
    def __init__(self, transformation = np.mat('0 1; 1 0', dtype = complex)):
        print("Creating a new gate");
        self.transformation = transformation;
        
    def processQubit(self, qubit):
        print((qubit.getCurrentState()));
        qubit.setCurrentState(np.asarray(self.transformation.dot(qubit.getCurrentState()))[0]);
        print(qubit.getCurrentState());
        
class HadamardGate(Gate):
    def __init__(self):
        Gate.__init__(self, 1/math.sqrt(2)*np.mat('1 1; 1 -1', dtype = complex));
        print("Creating a hadamard gate");
        
class PauliXGate(Gate):
    def __init__(self):
        Gate.__init__(self, np.mat('0 1; 1 0', dtype = complex));
        print("Creating a pauli-x gate");
        
class PauliYGate(Gate):
    def __init__(self):
        Gate.__init__(self, np.mat('0 -1j; 1j 0', dtype = complex));
        print("Creating a hadamard gate");
        
class PauliZGate(Gate):
    def __init__(self):
        Gate.__init__(self, np.mat('1 0; 0 -1', dtype = complex));
        print("Creating a hadamard gate");


# +
qubit = Qubit(np.array([0,1], dtype=complex));
myGate = HadamardGate();
myGate.processQubit(qubit);

qubit.measure();
# -



StarcoderdataJupyterScriptsDedupFiltered