def forward(self, input, normalise=False):
        half0 = self.gatelist[0].forward(input)
        half1 = self.gatelist[1].forward(input)
        tmp = unionlayer.join(half0, half1)
        tmp = unionlayer.join(tmp, self.ancilaries)
        for x in range(2, len(self.gatelist)):
            tmp = self.gatelist[x].forward(tmp)

        return tmp
    def forward(self, input):
        quarter0 = self.gatelist[0].forward(input)
        quarter1 = self.gatelist[1].forward(input)
        quarter2 = self.gatelist[2].forward(input)
        quarter3 = self.gatelist[3].forward(input)
        half0 = unionlayer.join(quarter0, quarter1)
        half1 = unionlayer.join(quarter2, quarter3)
        tmp = super().forward_two_inputs(half0, half1, 4)

        return tmp
    def forward(self, input, recursion=0):
        third0 = self.gatelist[0].forward(input)
        third1 = self.gatelist[1].forward(input)
        third2 = self.gatelist[2].forward(input)
        tmp = unionlayer.join(third0, third1)
        tmp = unionlayer.join(tmp, third2)

        for i in range(recursion):
            for x in range(3, len(self.gatelist)):
                tmp = self.gatelist[x].forward(tmp)

        return tmp
    def forward_two_inputs(self, input0, input1, start=0):
        half0 = self.gatelist[start].forward(input0)
        half1 = self.gatelist[start + 1].forward(input1)
        tmp = unionlayer.join(half0, half1)
        for x in range(start + 2, len(self.gatelist)):
            tmp = self.gatelist[x].forward(tmp)

        return tmp
    def forward(self, input, recursion=1):
        half0 = self.gatelist[0].forward(input)
        half1 = self.gatelist[1].forward(input)
        tmp = unionlayer.join(half0, half1)

        for i in range(recursion):
            for x in range(2, len(self.gatelist)):
                tmp = self.gatelist[x].forward(tmp)

        return tmp
Exemplo n.º 6
0
def fidelity_partial_list(output, target,nbqubitout,nbqutbittarget):
    #print(output)
    #print(target)
    partial=tf.ones([2**(nbqubitout-nbqutbittarget),tf.shape(output)[1]],dtype=output.dtype)
    #partialnorm=tf.div(partial, tf.norm(partial, axis=0))
    target2=unionlayer.join(partial,target)
    #print(target2)
    temp=tf.abs(tf.reduce_sum(tf.square(tf.abs(tf.einsum('ij,ij->ij', tf.conj(target2), output))), axis=0))
    #print(temp)
    return temp
def preprocess(input,target):
    #ctes=vectorencoder.gennormpluscte(castinput,3,cte=0.01)
    #ancinit = 3 * tf.ones_like(target)
    #ancinit2 = 63*tf.ones_like(target)
    #anc0 = unionlayer.inttoqubit(ancinit, 8)
    #anc1 = unionlayer.inttoqubit(ancinit2, 6)

    ctes = vectorencoder.gencte(input, 2, cte=0.0)
    #print(ctes)
    encodeddata=vectorencoder.encode_vectors(input,2,ctes)
    colvec =tf.transpose(encodeddata)
    #print(colvec.shape)
    c = unionlayer.join(colvec, colvec)
    #v = unionlayer.join(c, c)
    #w = unionlayer.join(v, v)
    #w = unionlayer.join(v, anc0 )
    #w = unionlayer.join(anc1, w)
    #w = unionlayer.join(c ,anc0 )
    return c
    init = tf.orthogonal_initializer
    #real = tf.get_variable("v", [2**8, 2**8], initializer=init)
    real = tf.eye(2**8, dtype="float32")
    imag = tf.zeros_like(real, dtype="float32")
    param = tf.complex(real, imag)

    learningrate = 0.01
    momentum = 0.01

    gate0 = genericQGate.genericQGate(param, 8, 8, 0, learningrate, momentum)

    gate1 = genericQGate.genericQGate(param, 8, 8, 0, learningrate, momentum)

    gate2 = genericQGate.genericQGate(param, 8, 8, 0, learningrate, momentum)

    copy0 = unionlayer.join(normedinput, normedinput)
    copy1 = unionlayer.join(copy0, normedinput)
    full = unionlayer.join(
        copy1,
        unionlayer.inttoqubit(tf.zeros_like(targetbatch, dtype="float32"), 2))

    o0 = gate0.forward(full)

    for x in range(0, 6):
        o0, r = lazymeasure.lazymeasure(o0)

    full1 = unionlayer.join(copy1, o0)
    o1 = gate1.forward(full1)

    for x in range(0, 6):
        o1, r = lazymeasure.lazymeasure(o1)
Exemplo n.º 9
0
    label=[[1],[0]]

    #print(fidelity_partial_list(un,zero,2,1))
    fd_list=fidelity_partial_list(un, zero, 2, 1)
    print(fd_list)
    #print(true_max_metric(fd_list))
    # print(cross_entropy(un, zero,2,1))
    # label=tf.transpose(tf.cast([0.0,1.0], dtype="float32"))
    #print(maxclass(un, label, 2, 1, 2, 'inttoqubit'))
    #print(true_max_metric(un,label,2,1,'inttoqubit'))


    real = [[1 / tf.sqrt(2.0), 1 / tf.sqrt(2.0)],[1 / tf.sqrt(2.0), 1 / tf.sqrt(2.0)]]
    imag = [[0.0, 0.0],[0.0, 0.0]]
    plus = tf.transpose(tf.complex(real, imag))
    temp=unionlayer.join(plus,plus)

    testtarget0=unionlayer.inttoqubit([0.0,0.0], 1)
    testtarget1 = unionlayer.inttoqubit([1.0, 1.0], 1)
    tep=tf.random.uniform([256,2])

    test3=tf.transpose(unionlayer.state_activation_0(tf.transpose(tep)))
    print(test3)
    print(np.linalg.norm(test3, ord=2, axis=0))

    test4=tf.transpose(tf.nn.softmax(tf.transpose(tep)))
    print(test4)
    print(np.sum(test4,axis=0))

    #print(test3)
    #print(test)
    #print(y)
    #print(lf.shape)
    #print(l1)
    #print(lf)
    #print(tf.norm(lf,axis=1))
    #tmp=1 /tf.norm(lf, axis=1)
    #tmp2=tf.einsum('ij,i->ij',lf,tmp)
    #print("lol")
    #print(tmp2)
    #lfn=tf.div(lf,tf.norm(lf,2,axis =1))
    #print(lfn.shape)
    #print(tf.norm(tmp2, axis=1))
    nninput = tf.transpose(lf)
    #sth=unionlayer.join(anc0,nninput )
    #st=unionlayer.join(sth,anc1)
    st = unionlayer.join(anc0, nninput)

    #cir = subcircuit.ArityFillCircuit(nbqubits, 8, 6, "test0", learningrate, momentum)
    #cir = subcircuit.ArityFillCircuit(nbqubits, aritycircuitsize, aritycircuitdepth, "test0", learningrate, momentum)

    #cir = subcircuit.ArityHalfCircuit(nbqubits, aritycircuitsize, aritycircuitdepth, "test0", learningrate, momentum)

    cir = subcircuit.QuincunxCircuit(nbqubits, aritycircuitsize,
                                     aritycircuitdepth, "test0")

    #out = cir.forward_two_inputs(nninput,stateinput)
    #out = cir.forward_two_inputs(nninput, nninput)
    #out = cir.forward(nninput)
    out = cir.forward(st, 1)
    #for x in range(0,8):
    gate2 = genericQGate.genericQGate(phasechange, 1, 1, 0, learningrate,
                                      momentum)

    #for i in range(8):

    real = tf.random_normal([2, batchsize - 2], 0, 1)
    imag = tf.random_normal([2, batchsize - 2], 0, 1)
    #real = [[0.0,  1.0]]
    #imag = [[0.0, 0.0]]

    temp = tf.complex(real, imag)
    vectorrand = tf.div(temp, tf.norm(temp, axis=0))
    vectorrand = tf.concat([tf.transpose(param1), vectorrand], axis=1)

    prep = unionlayer.join(vectorrand, phipluscte)

    #o0 = gateh.forward(gate0.forward(prep))
    o0 = gatep1.forward(gatep.forward(prep))

    (o1zero, r0zero), (o1un, r0un) = lazymeasure.lazymeasureboth(o0)

    (o2zz, r1zz), (o2zu, r1zu) = lazymeasure.lazymeasureboth(o1zero)

    (o2uz, r1uz), (o2uu, r1uu) = lazymeasure.lazymeasureboth(o1un)

    o2 = tf.concat(
        [o2zz,
         gate1.forward(o2zu), o2uz,
         gate2.forward(gate1.forward(o2uu))],
        axis=1)