Exemple #1
0
    def get_matrix(self):
        size = 2**(len(self))
        result = Matrix([[0] * size for x in range(size)])
        for i in range(size):
            for j in range(size):
                result[i][j] = self.matrix[i][j]

        return result
Exemple #2
0
    def get_subcircuit(self, name):
        self.sort()

        states = Matrix.Id(2**(len(self)))

        for g_i in range(len(self.gates)):
            gate = self.gates[g_i]
            startqbits = [self.startqbit(g_i, i) for i in range(len(gate))]
            permutation = [i for i in range(len(self))]
            permuted = []
            for i in range(len(startqbits)):
                x = permutation[i]
                y = startqbits[i]
                if x != y and x not in permuted:
                    permutation[i] = y
                    permutation[y] = x
                    permuted.append(y)

            P1 = Matrix.Permutation(permutation)
            P2 = P1.getConjugate()

            if len(self) == len(gate):
                M = gate.get_matrix()
            else:
                M = tensor(
                    [gate.get_matrix(),
                     Matrix.Id(2**(len(self) - len(gate)))])

            states = P2 * states
            states = M * states
            states = P1 * states

        startqbits = self.startqbits()
        P1 = Matrix.Permutation(startqbits)
        P1.transpose()
        states = P1 * states

        return Gate(states, name)
from main.circuit import*
from main.matrix import Matrix, tensor
from math import pi, sqrt

#Grovers algorithm finds an x for which the function f: {0,1,...,2^n -1}-->{0,1} is 1.
#It depends on the function f. We define f with the variable x, so f(13)=1 and 0 otherwise
x=13

#2^n is the size of the domain of f. n+1 is then the size of the circuit (we need 1 extra qbit)
n=6
c=Circuit(n+1)

#define the matrix that computes f
F=Matrix.Zero(2**(n+1))
for i in range(2**(n+1)):
    for j in range(2**(n+1)):
        if i==j and i!=2*x and i!=(2*x)+1:
            F[i][j]=1
        elif i==2*x and j==(2*x)+1:
            F[i][j]=1
        elif i==(2*x)+1 and j==2*x:
            F[i][j]=1
             
U=Matrix.Zero(2**(n))
for i in range(2**(n)):
    if i==0:
        U[i][i]=1
    else:
        U[i][i]=-1
         
U=tensor([U,Matrix.Id(2)])
Exemple #4
0
 def __init__(self, name="T"):
     super().__init__(Matrix.T(), name, True)
Exemple #5
0
 def __init__(self, name="CNot"):
     super().__init__(Matrix.Cnot(), name, True)
Exemple #6
0
 def __init__(self, size=1, name="QFT"):
     super().__init__(Matrix.QFT(size), name, True)
Exemple #7
0
 def __init__(self, size=1, name="SqrtNot"):
     super().__init__(Matrix.SqrtNot(size), name, True)
Exemple #8
0
 def __init__(self, size=1, name="H"):
     super().__init__(Matrix.H(size), name, True)
Exemple #9
0
    def run_method2(self, in_v):
        self.sort()

        if len(in_v) != len(self):
            raise RuntimeError(
                "Input length does not match the size of the circuit.")
        for i in in_v:
            if i == 0 or i == 1:
                pass
            else:
                raise RuntimeError("Invalid input - values must be 0 or 1.")

        states = []
        weights = []
        for i in in_v:
            if i == 0:
                states.append(Matrix.vector([1, 0]))
            elif i == 1:
                states.append(Matrix.vector([0, 1]))

        if len(states) > 1:
            states = tensor(states)
        else:
            states = states[0]

        for g_i in range(len(self.gates)):
            gate = self.gates[g_i]
            startqbits = [self.startqbit(g_i, i) for i in range(len(gate))]
            permutation = [i for i in range(len(self))]
            for i in range(len(startqbits)):
                x = permutation[i]
                y = startqbits[i]
                if x != y:
                    j = permutation.index(y)
                    permutation[i] = y
                    permutation[j] = x

            P1 = Matrix.Permutation(permutation)
            if not P1.isUnitary():
                print("permutation matrix seems to be wrong")
            P2 = P1.getConjugate()

            if len(self) == len(gate):
                M = gate.get_matrix()
            else:
                M = tensor(
                    [gate.get_matrix(),
                     Matrix.Id(2**(len(self) - len(gate)))])

            states = P2 * states
            states = M * states
            states = P1 * states

        startqbits = self.startqbits()
        P1 = Matrix.Permutation(startqbits)
        P1.transpose()
        states = P1 * states

        for x in range(len(states)):
            weights.append((abs(states[x][0]))**2)

        return weights