def test_canonical_decomposition():
    for tt1 in range(0, 10):
        for tt2 in range(tt1):
            for tt3 in range(tt2):
                t1, t2, t3 = tt1 / 20, tt2 / 20, tt3 / 20
                if t3 == 0 and t1 > 0.5:
                    continue
                coords = np.asarray((t1, t2, t3))

                print('b')
                circ0 = qf.Circuit()
                circ0 += qf.ZYZ(0.2, 0.2, 0.2, q0=0)
                circ0 += qf.ZYZ(0.3, 0.3, 0.3, q0=1)
                circ0 += qf.CANONICAL(t1, t2, t3, 0, 1)
                circ0 += qf.ZYZ(0.15, 0.2, 0.3, q0=0)
                circ0 += qf.ZYZ(0.15, 0.22, 0.3, q0=1)
                gate0 = circ0.asgate()
                print('c')

                circ1 = qf.canonical_decomposition(gate0)
                assert qf.gates_close(gate0, circ1.asgate())
                print('d')

                print(circ1)
                canon = circ1.elements[6]
                new_coords = np.asarray(
                    [canon.params[n] for n in ['tx', 'ty', 'tz']])
                assert np.allclose(coords, np.asarray(new_coords))

                coords2 = qf.canonical_coords(gate0)
                assert np.allclose(coords, np.asarray(coords2))
                print('>')
                print()
示例#2
0
def fit_zyz(target_gate):
    """
    Tensorflow example. Given an arbitrary one-qubit gate, use gradient
    descent to find corresponding parameters of a universal ZYZ gate.
    """

    assert bk.BACKEND == 'tensorflow'

    tf = bk.TL
    steps = 4000

    t = tf.get_variable('t', [3])
    gate = qf.ZYZ(t[0], t[1], t[2])

    ang = qf.fubini_study_angle(target_gate.vec, gate.vec)
    opt = tf.train.AdamOptimizer(learning_rate=0.001)
    train = opt.minimize(ang, var_list=[t])

    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)

        for step in range(steps):
            sess.run(train)
            loss = sess.run(ang)
            sys.stdout.write('\r')
            sys.stdout.write("step: {} gate_angle: {}".format(step, loss))
            if loss < 0.0001:
                break
        print()
        return sess.run(t)
示例#3
0
def test_ZYZ():
    t0 = random.uniform(-2, +2)
    t1 = random.uniform(-2, +2)
    t2 = random.uniform(-2, +2)

    gate = qf.ZYZ(t0, t1, t2, 0)
    assert qf.almost_unitary(gate)
    inv = gate.H
    assert type(gate) == type(inv)
    assert qf.gates_close(qf.I(0), inv @ gate)
示例#4
0
def test_gatepow():
    gates = [
        qf.I(),
        qf.X(),
        qf.Y(),
        qf.Z(),
        qf.H(),
        qf.S(),
        qf.T(),
        qf.PHASE(0.1),
        qf.RX(0.2),
        qf.RY(0.3),
        qf.RZ(0.4),
        qf.CZ(),
        qf.CNOT(),
        qf.SWAP(),
        qf.ISWAP(),
        qf.CPHASE00(0.5),
        qf.CPHASE01(0.6),
        qf.CPHASE10(0.6),
        qf.CPHASE(0.7),
        qf.PSWAP(0.15),
        qf.CCNOT(),
        qf.CSWAP(),
        qf.TX(2.7),
        qf.TY(1.2),
        qf.TZ(0.3),
        qf.ZYZ(3.5, 0.9, 2.1),
        qf.CANONICAL(0.1, 0.2, 7.4),
        qf.XX(1.8),
        qf.YY(0.9),
        qf.ZZ(0.45),
        qf.PISWAP(0.2),
        qf.EXCH(0.1),
        qf.TH(0.3)
    ]

    for gate in gates:
        assert qf.gates_close(gate.H, gate**-1)

    for gate in gates:
        sqrt_gate = gate**(1 / 2)
        two_gate = sqrt_gate @ sqrt_gate
        assert qf.gates_close(gate, two_gate)

    for gate in gates:
        gate0 = gate**0.3
        gate1 = gate**0.7
        gate2 = gate0 @ gate1
        assert qf.gates_close(gate, gate2)

    for K in range(1, 5):
        gate = qf.random_gate(K)  # FIXME: Throw error on K=0
        sqrt_gate = gate**0.5
        two_gate = sqrt_gate @ sqrt_gate
        assert qf.gates_close(gate, two_gate)

    for gate in gates:
        rgate = qf.Gate((gate**0.5).tensor)
        tgate = rgate @ rgate
        assert qf.gates_close(gate, tgate)
示例#5
0
def test_zyz_circuit():
    gate0 = qf.zyz_circuit(0.1, 0.3, 0.2, 0).asgate()
    gate1 = qf.ZYZ(0.1, 0.3, 0.2, q0=0)
    assert qf.gates_close(gate0, gate1)
示例#6
0
def test_asgate():
    circ = qf.zyz_circuit(0.1, 2.2, 0.5, 0)
    print(">>>>", circ, len(circ.elements))
    assert qf.gates_close(circ.asgate(), qf.ZYZ(0.1, 2.2, 0.5))
示例#7
0
def test_repr2():
    g = qf.ZYZ(3.0, 1.0, 1.0, 0)
    assert str(g) == 'ZYZ(3, 1, 1) 0'
示例#8
0
 def loss_fn():
     """Loss"""
     gate = qf.ZYZ(t[0], t[1], t[2])
     ang = qf.fubini_study_angle(target_gate.vec, gate.vec)
     return ang
示例#9
0
def test_asgate():
    gate0 = qf.ZYZ(0.1, 2.2, 0.5)
    circ0 = qf.zyz_circuit(0.1, 2.2, 0.5, 0)
    dag0 = qf.DAGCircuit(circ0)
    gate1 = dag0.asgate()
    assert qf.gates_close(gate0, gate1)