def objective_function(self, params):        
        U_ = self.gate(params)
        U = self.u
        A = unitary_to_tensor(cirq.unitary(U))
        A_ = unitary_to_tensor(cirq.unitary(U_))
        
        _, r = Map(A,A_).right_fixed_point()
        
        R = Environment(put_env_on_left_site(r), 'R')
        L = Environment(put_env_on_right_site(r.conj().T), 'L')
        
        W = Environment(W, 'W')

        qbs = cirq.LineQubit.range(6)
        C = cirq.Circuit.from_ops([cirq.H(qbs[3]), cirq.CNOT(*qbs[3:5]),
                                   U(*qbs[2:4]),
                                   U(*qbs[1:3]),
                                   W(*qbs[2:4]),
                                   L(*qbs[0:2]),
                                   R(*qbs[4:]),
                                   cirq.inverse(U_)(*qbs[1:3]),
                                   cirq.inverse(U_)(*qbs[2:4]),
                                   cirq.CNOT(*qbs[3:5]), cirq.H(qbs[3])])

        s = cirq.Simulator(dtype=np.complex128)
        return -np.abs(s.simulate(C).final_state[0])*2
def obj_state(p_, A, WW):
    p, rs = p_[:15], p_[15:]
    
    B = iMPS([unitary_to_tensor(cirq.unitary(gate(p)))]).left_canonicalise()[0]
    U = Environment(tensor_to_unitary(A), 'U')
    U_ = Environment(tensor_to_unitary(B), 'U\'')
    
    R = state_gate(rs)
    #R = Environment(environment_to_unitary(r), 'R')
    L = Environment(put_env_on_right_site(environment_from_unitary(cirq.unitary(R)).conj().T), 'L')
    
    W = Environment(WW, 'W')
    
    qbs = cirq.LineQubit.range(5)
    C = cirq.Circuit.from_ops([R(*qbs[3:5]),
                               U(*qbs[2:4]),
                               U(*qbs[1:3]),
                               W(*qbs[2:4]),
                               L(*qbs[0:2]),
                               cirq.inverse(U_)(*qbs[1:3]),
                               cirq.inverse(U_)(*qbs[2:4]),
                               cirq.CNOT(*qbs[3:5]), cirq.H(qbs[3])])
    
    s = cirq.Simulator(dtype=np.complex128)
    return s.simulate(C).final_state
def obj(p_, A, WW):
    p, rs = p_[:15], p_[15:]
    
    B = iMPS([unitary_to_tensor(cirq.unitary(gate(p)))]).left_canonicalise()[0]

    U = Environment(tensor_to_unitary(A), 'U')
    U_ = Environment(tensor_to_unitary(B), 'U\'')

    E = Map(np.tensordot(WW, merge(A, A), [1, 0]), merge(B, B))
    x,r = E.right_fixed_point()
    x_,l = E.left_fixed_point()
    
    R = Environment(put_env_on_left_site(r), 'θR')
    L = Environment(put_env_on_right_site(r.conj().T), 'θL')
    
    W = Environment(WW, 'W')
    
    qbs = cirq.LineQubit.range(5)
    C = cirq.Circuit.from_ops([R(*qbs[3:5]),
                               U(*qbs[2:4]),
                               U(*qbs[1:3]),
                               W(*qbs[2:4]),
                               L(*qbs[0:2]),
                               cirq.inverse(U_)(*qbs[1:3]),
                               cirq.inverse(U_)(*qbs[2:4]),
                               cirq.CNOT(*qbs[3:5]), cirq.H(qbs[3])])
    
    s = cirq.Simulator(dtype=np.complex128)
    return -np.sqrt(np.abs(np.sqrt(2)*s.simulate(C).final_state[0]))
Exemple #4
0
def obj(p, A, WW):
    B = iMPS([unitary_to_tensor(cirq.unitary(gate(p)))]).left_canonicalise()[0]
    WW_ = np.eye(WW.shape[0])

    E = Map(np.tensordot(WW, merge(A, A), [1, 0]), merge(B, B))

    x, r = E.right_fixed_point()
    x_, l = E.left_fixed_point()
    l = r

    U = Environment(tensor_to_unitary(A), 'U')
    U_ = Environment(tensor_to_unitary(B), 'U\'')

    R = Environment(put_env_on_left_site(r), 'θR')
    left = put_env_on_right_site(l.conj().T)
    L = Environment(left, 'θL')

    W = Environment(WW, 'W')

    qbs = cirq.LineQubit.range(6)
    C = cirq.Circuit([
        cirq.H(qbs[3]),
        cirq.CNOT(*qbs[3:5]),
        U(*qbs[2:4]),
        U(*qbs[1:3]),
        W(*qbs[2:4]),
        L(*qbs[0:2]),
        R(*qbs[4:]),
        cirq.inverse(U_)(*qbs[1:3]),
        cirq.inverse(U_)(*qbs[2:4]),
        cirq.CNOT(*qbs[3:5]),
        cirq.H(qbs[3])
    ])
    #qbs = cirq.LineQubit.range(4)
    #normC = cirq.Circuit.from_ops([cirq.H(qbs[1]),
    #                               cirq.CNOT(*qbs[1:3]),
    #                               L(*qbs[:2]),
    #                               R(*qbs[-2:]),
    #                               cirq.CNOT(*qbs[1:3]),
    #                               cirq.H(qbs[1])
    #                               ])
    s = cirq.Simulator(dtype=np.complex128)
    ff = np.sqrt(
        2 * np.abs(s.simulate(C).final_state[0])
    )  #/np.abs(s.simulate(normC).final_state[0])), np.sqrt(np.abs(x[0]))
    #print(ff[0]-ff[1])
    #print(ff[0], ff[1])
    return -ff
Exemple #5
0
def scars_cost_fun_alternate(params, current_params, ham):
    '''
    This cost function doesn't use the quantum circuit parameterisation
    
    params are formatted like: [θ1, ϕ1, ϕ2, θ2], for convenience with the classical differential eqn solver
    '''
    θ1, ϕ1, ϕ2, θ2 = current_params
    θ1_, ϕ1_, ϕ2_, θ2_ = params

    A1 = A(θ1, ϕ1)
    A2 = A(θ2, ϕ2)
    A1_ = A(θ1_, ϕ1_)
    A2_ = A(θ2_, ϕ2_)

    A12 = merge(A1, A2)
    A12_ = merge(A1_, A2_)

    _, r = Map(A12, A12_).right_fixed_point()
    R = Environment(put_env_on_left_site(r), 'R')
    L = Environment(put_env_on_right_site(r.conj().T), 'L')

    U12 = Tensor(tensor_to_unitary(A12), 'U')
    U12_ = Tensor(tensor_to_unitary(A12_), 'U\'')

    q = cirq.LineQubit.range(8)
    circuit = cirq.Circuit.from_ops([
        cirq.H(q[5]),
        cirq.CNOT(q[5], q[6]),
        U12(*q[3:6]),
        U12(*q[1:4]),
        L(*q[0:2]),
        ham(*q[2:6]),
        R(*q[6:8]),
        cirq.inverse(U12_(*q[1:4])),
        cirq.inverse(U12_(*q[3:6])),
        cirq.CNOT(q[5], q[6]),
        cirq.H(q[5])
    ])

    # print(circuit.to_text_diagram(transpose = True))
    sim = cirq.Simulator()
    ψ = sim.simulate(circuit).final_state[0]
    return -np.abs(ψ) * 2
Exemple #6
0
def scars_time_evolve_cost_function(params, current_params, ham):
    '''
    params are formatted like: [θ1, ϕ1, ϕ2, θ2], for convenience with the classical differential eqn solver
    '''
    θ1, ϕ1, ϕ2, θ2 = current_params
    θ1_, ϕ1_, ϕ2_, θ2_ = params

    A1 = A(θ1, ϕ1)
    A2 = A(θ2, ϕ2)
    A1_ = A(θ1_, ϕ1_)
    A2_ = A(θ2_, ϕ2_)

    _, r = Map(merge(A1, A2), merge(A1_, A2_)).right_fixed_point()
    R = Environment(put_env_on_left_site(r), 'R')
    L = Environment(put_env_on_right_site(r.conj().T), 'L')

    U12 = ScarGate(current_params)
    U12_ = ScarGate(params)
    q = cirq.LineQubit.range(8)
    circuit = cirq.Circuit.from_ops([
        cirq.H(q[5]),
        cirq.CNOT(q[5], q[6]),
        U12(*q[3:6]),
        U12(*q[1:4]),
        L(*q[0:2]),
        ham(*q[2:6]),
        R(*q[6:8]),
        cirq.inverse(U12_(*q[1:4])),
        cirq.inverse(U12_(*q[3:6])),
        cirq.CNOT(q[5], q[6]),
        cirq.H(q[5])
    ])

    # print(circuit.to_text_diagram(transpose = True))
    sim = cirq.Simulator()
    ψ = sim.simulate(circuit).final_state[0]
    return -np.abs(ψ) * 2
    def obj(rs, initial_params=initial, WW=None):
        WW = np.eye(4) if WW is None else WW

        A = iMPS([unitary_to_tensor(cirq.unitary(gate(p1)))
                  ]).left_canonicalise()[0]
        B = iMPS([unitary_to_tensor(cirq.unitary(gate(p2)))
                  ]).left_canonicalise()[0]

        U = Environment(tensor_to_unitary(A), 'U')
        U_ = Environment(tensor_to_unitary(B), 'U\'')

        r = rotate_to_hermitian((rs[:4] + 1j * rs[4:]).reshape(2, 2))
        r /= np.sqrt(np.trace(r.conj().T @ r))
        l = r

        R = Environment(put_env_on_left_site(r), 'R')
        L = Environment(put_env_on_right_site(l.conj().T), 'L')

        W = Environment(WW, 'W')

        qbs = cirq.LineQubit.range(6)
        C = cirq.Circuit.from_ops([
            cirq.H(qbs[3]),
            cirq.CNOT(*qbs[3:5]),
            U(*qbs[2:4]),
            U(*qbs[1:3]),
            W(*qbs[2:4]),
            L(*qbs[0:2]),
            R(*qbs[4:]),
            cirq.inverse(U_)(*qbs[1:3]),
            cirq.inverse(U_)(*qbs[2:4]),
            cirq.CNOT(*qbs[3:5]),
            cirq.H(qbs[3])
        ])

        s = cirq.Simulator(dtype=np.complex128)
        return -np.abs(s.simulate(C).final_state[0]) * 2
            get_env_off_left_site(np.prod(put_env_on_left_site(q,
                                                               ret_n=True))),
            q)
        assert np.allclose(
            get_env_off_right_site(
                np.prod(put_env_on_right_site(q, ret_n=True))), q)
        U = put_env_on_left_site(q)
        V = put_env_on_right_site(q)
        assert np.allclose(V.conj().T @ V, np.eye(U.shape[0]))
        assert np.allclose(U.conj().T @ U, np.eye(U.shape[0]))

    for _ in range(N):
        A = iMPS().random(2, 2).left_canonicalise()[0]
        B = iMPS().random(
            2,
            2).left_canonicalise()[0]  #np.tensordot(expm(-1j*Z*dt), A, [1, 0])

        U = Environment(tensor_to_unitary(A), 'U')
        U_ = Environment(tensor_to_unitary(B), 'U\'')

        x, r = Map(merge(A, A), merge(B, B)).right_fixed_point()
        x_, l = Map(merge(A, A), merge(B, B)).left_fixed_point()
        L = put_env_on_right_site(l)
        R = put_env_on_left_site(r)
        assert np.allclose(get_env_off_left_site(put_env_on_left_site(r)), r)
        assert np.allclose(get_env_off_right_site(put_env_on_right_site(l)), l)
        U = put_env_on_left_site(r)
        V = put_env_on_right_site(l)
        assert np.allclose(V.conj().T @ V, np.eye(U.shape[0]))
        assert np.allclose(U.conj().T @ U, np.eye(U.shape[0]))
def run_tests(N):
    """run_tests: just a whole bunch of tests.

    :param N: number of iterations to run
    """
    for _ in range(N):
        q = np.random.randn(2, 2)+1j*np.random.randn(2, 2)
        assert np.allclose(get_env_off_left_site(np.prod(put_env_on_left_site(q, ret_n=True))), q)
        assert np.allclose(get_env_off_right_site(np.prod(put_env_on_right_site(q, ret_n=True))), q)
        U = put_env_on_left_site(q)
        V = put_env_on_right_site(q)
        assert np.allclose(V.conj().T@V, np.eye(U.shape[0]))
        assert np.allclose(U.conj().T@U, np.eye(U.shape[0]))
        
        A = iMPS().random(2, 2).left_canonicalise()[0]
        B = iMPS().random(2, 2).left_canonicalise()[0]#np.tensordot(expm(-1j*Z*dt), A, [1, 0])

        U = Environment(tensor_to_unitary(A), 'U')
        U_ = Environment(tensor_to_unitary(B), 'U\'')

        x, r = Map(merge(A, A), merge(B, B)).right_fixed_point()
        x_, l = Map(merge(A, A), merge(B, B)).left_fixed_point()
        L = put_env_on_right_site(l)
        R = put_env_on_left_site(r)
        assert np.allclose(get_env_off_left_site(put_env_on_left_site(r)), r)
        assert np.allclose(get_env_off_right_site(put_env_on_right_site(l)), l)
        U = put_env_on_left_site(r)
        V = put_env_on_right_site(l)
        assert np.allclose(V.conj().T@V, np.eye(U.shape[0]))
        assert np.allclose(U.conj().T@U, np.eye(U.shape[0]))

        A = iMPS().random(2, 2).left_canonicalise()[0]
        B = iMPS().random(2, 2).left_canonicalise()[0]#np.tensordot(expm(-1j*Z*dt), A, [1, 0])

        E = Map(A, B)

        x, r = E.right_fixed_point()
        x_, l = E.left_fixed_point()


        U = Environment(tensor_to_unitary(A), 'U')
        U_ = Environment(tensor_to_unitary(B), 'U\'')

        R = Environment(put_env_on_left_site(r), 'R')
        L = Environment(put_env_on_right_site(l.conj().T), 'L')


        qbs = cirq.LineQubit.range(4)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[1]), cirq.CNOT(*qbs[1:3]), 
                                       R(*qbs[2:]),
                                       g[0](qbs[1]),
                                       cirq.CNOT(*qbs[1:3]), cirq.H(qbs[1])])
            s = cirq.Simulator()
            assert np.allclose(2*s.simulate(C).final_state[0]-np.trace(g[1]@r), 0, 1e-6, 1e-6)
        # r is the matrix on the 1st qubit

        qbs = cirq.LineQubit.range(4)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[1]), cirq.CNOT(*qbs[1:3]),
                                       U(*qbs[0:2]),
                                       R(*qbs[2:]),
                                       g[0](qbs[0]),
                                       cirq.inverse(U_)(*qbs[0:2]),
                                       cirq.CNOT(*qbs[1:3]), cirq.H(qbs[1])])
            s = cirq.Simulator()
            assert np.allclose(2*s.simulate(C).final_state[0]-x*np.trace(g[1]@r), 0, 1e-6, 1e-6)

        qbs = cirq.LineQubit.range(5)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[2]), cirq.CNOT(*qbs[2:4]),
                                       U(*qbs[1:3]),
                                       U(*qbs[0:2]),
                                       R(*qbs[3:]),
                                       g[0](qbs[0]),
                                       cirq.inverse(U_)(*qbs[0:2]),
                                       cirq.inverse(U_)(*qbs[1:3]),
                                       cirq.CNOT(*qbs[2:4]), cirq.H(qbs[2])])
            s = cirq.Simulator()
            #print(C.to_text_diagram(transpose=True))
            #raise Exception
            assert np.allclose(2*s.simulate(C).final_state[0]-x**2*np.trace(g[1]@r), 0, 1e-6, 1e-6)


        qbs = cirq.LineQubit.range(3)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[1]), cirq.CNOT(*qbs[1:3]), 
                                       L(*qbs[:2]),
                                       g[0](qbs[2]),
                                       cirq.CNOT(*qbs[1:3]), cirq.H(qbs[1])])
            s = cirq.Simulator()

            assert np.allclose(2*s.simulate(C).final_state[0]-np.trace(g[1]@l.conj()), 0, 1e-6, 1e-6)
        # r is the matrix on the 1st qubit

        qbs = cirq.LineQubit.range(4)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[2]), cirq.CNOT(*qbs[2:4]),
                                       U(*qbs[1:3]),
                                       L(*qbs[:2]),
                                       g[0](qbs[3]),
                                       cirq.inverse(U_)(*qbs[1:3]),
                                       cirq.CNOT(*qbs[2:4]), cirq.H(qbs[2])])
            s = cirq.Simulator()
            #print(C.to_text_diagram(transpose=True))
            #raise Exception
            assert np.allclose(2*s.simulate(C).final_state[0]-x*np.trace(g[1]@l.conj()), 0, 1e-6, 1e-6)

        qbs = cirq.LineQubit.range(5)
        for g in zip([cirq.I, cirq.X, cirq.Y, cirq.Z], [I, X, Y, Z]):
            C = cirq.Circuit.from_ops([cirq.H(qbs[3]), cirq.CNOT(*qbs[3:5]),
                                       U(*qbs[2:4]),
                                       U(*qbs[1:3]),
                                       L(*qbs[0:2]),
                                       g[0](qbs[4]),
                                       cirq.inverse(U_)(*qbs[1:3]),
                                       cirq.inverse(U_)(*qbs[2:4]),
                                       cirq.CNOT(*qbs[3:5]), cirq.H(qbs[3])])
            s = cirq.Simulator()
            assert np.allclose(2*s.simulate(C).final_state[0]-x**2*np.trace(g[1]@l.conj()), 0, 1e-6, 1e-6)

        qbs = cirq.LineQubit.range(6)
        C = cirq.Circuit.from_ops([cirq.H(qbs[3]), cirq.CNOT(*qbs[3:5]),
                                   U(*qbs[2:4]),
                                   U(*qbs[1:3]),
                                   L(*qbs[0:2]),
                                   R(*qbs[4:]),
                                   cirq.inverse(U_)(*qbs[1:3]),
                                   cirq.inverse(U_)(*qbs[2:4]),
                                   cirq.CNOT(*qbs[3:5]), cirq.H(qbs[3])])
        s = cirq.Simulator()
        assert np.allclose(2*s.simulate(C).final_state[0], x**2*np.trace(l.conj().T@r))