Ejemplo n.º 1
0
def test_fubini_study_angle():

    for _ in range(REPS):
        theta = random.uniform(-pi, +pi)

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RX(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RY(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec, qf.RZ(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(
            qf.fubini_study_angle(qf.SWAP().vec,
                                  qf.PSWAP(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

        ang = qf.asarray(qf.fubini_study_angle(qf.I().vec,
                                               qf.PHASE(theta).vec))
        assert 2 * ang / abs(theta) == ALMOST_ONE

    for n in range(1, 6):
        eye = qf.identity_gate(n)
        assert qf.asarray(qf.fubini_study_angle(eye.vec, eye.vec)) \
            == ALMOST_ZERO

    with pytest.raises(ValueError):
        qf.fubini_study_angle(qf.random_gate(1).vec, qf.random_gate(2).vec)
Ejemplo n.º 2
0
def test_fubini_study_angle() -> None:

    for _ in range(REPS):
        theta = random.uniform(-np.pi, +np.pi)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rx(theta, 0).su().tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Ry(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.Rz(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.Swap(0, 1).tensor, qf.PSwap(theta, 0, 1).tensor)

        assert np.isclose(2 * ang / abs(theta), 1.0)

        ang = qf.fubini_study_angle(qf.I(0).tensor, qf.PhaseShift(theta, 0).tensor)
        assert np.isclose(2 * ang / abs(theta), 1.0)

        assert qf.fubini_study_close(qf.Rz(theta, 0).tensor, qf.Rz(theta, 0).tensor)

    for n in range(1, 6):
        eye = qf.IdentityGate(list(range(n)))
        assert np.isclose(qf.fubini_study_angle(eye.tensor, eye.tensor), 0.0)

    with pytest.raises(ValueError):
        qf.fubini_study_angle(qf.RandomGate([1]).tensor, qf.RandomGate([0, 1]).tensor)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
def test_fubini_study_angle_states() -> None:
    # The state angle is half angle in Bloch sphere
    angle1 = 0.1324
    ket1 = qf.zero_state(1)
    ket2 = qf.Rx(angle1, 0).run(ket1)
    angle2 = qf.fubini_study_angle(ket1.tensor, ket2.tensor)
    assert np.isclose(angle1, angle2 * 2)
Ejemplo n.º 5
0
def test_fidelity() -> None:
    rho0 = qf.random_density(4)
    rho1 = qf.random_density(4)

    fid = qf.fidelity(rho0, rho1)
    assert 0.0 <= fid <= 1.0

    rho2 = qf.random_density([3, 2, 1, 0])
    fid = qf.fidelity(rho0, rho2)
    assert 0.0 <= fid <= 1.0

    fid = qf.fidelity(rho0, rho0)
    assert np.isclose(fid, 1.0)

    ket0 = qf.random_state(3)
    ket1 = qf.random_state(3)
    fid0 = qf.state_fidelity(ket0, ket1)

    rho0 = ket0.asdensity()
    rho1 = ket1.asdensity()
    fid1 = qf.fidelity(rho0, rho1)

    assert np.isclose(fid1, fid0)

    fid2 = np.cos(qf.fubini_study_angle(ket0.tensor, ket1.tensor)) ** 2
    assert np.isclose(fid2, fid0)
Ejemplo n.º 6
0
def test_fidelity():
    rho0 = qf.random_density(4)
    rho1 = qf.random_density(4)

    fid = qf.fidelity(rho0, rho1)
    print('FID', fid)
    assert 0.0 <= fid <= 1.0

    rho2 = qf.random_density([3, 2, 1, 0])
    fid = qf.fidelity(rho0, rho2)
    assert 0.0 <= fid <= 1.0

    fid = qf.fidelity(rho0, rho0)
    print('FID', fid)
    assert fid == ALMOST_ONE

    ket0 = qf.random_state(3)
    ket1 = qf.random_state(3)
    fid0 = qf.state_fidelity(ket0, ket1)

    rho0 = ket0.asdensity()
    rho1 = ket1.asdensity()
    fid1 = qf.fidelity(rho0, rho1)

    assert qf.asarray(fid1 - fid0) == ALMOST_ZERO

    fid2 = bk.cos(qf.fubini_study_angle(ket0.vec, ket1.vec))**2
    assert qf.asarray(fid2 - fid0) == ALMOST_ZERO
Ejemplo n.º 7
0
def test_fubini_study_angle_states():
    # The state angle is half angle in Bloch sphere
    angle1 = 0.1324
    ket1 = qf.zero_state(1)
    ket2 = qf.RX(angle1, 0).run(ket1)
    angle2 = qf.asarray(qf.fubini_study_angle(ket1.vec, ket2.vec))
    assert angle1 - angle2 * 2 == ALMOST_ZERO
Ejemplo n.º 8
0
def test_bures_angle() -> None:
    rho = qf.random_density(4)
    assert np.isclose(qf.bures_angle(rho, rho), 0.0, atol=ATOL * 10)

    rho1 = qf.random_density(4)
    qf.bures_angle(rho, rho1)

    ket0 = qf.random_state(4)
    ket1 = qf.random_state(4)
    rho0 = ket0.asdensity()
    rho1 = ket1.asdensity()

    ang0 = qf.fubini_study_angle(ket0.tensor, ket1.tensor)
    ang1 = qf.bures_angle(rho0, rho1)

    assert np.isclose(ang0, ang1)
Ejemplo n.º 9
0
def test_bures_angle():
    rho = qf.random_density(4)
    assert qf.bures_angle(rho, rho) == ALMOST_ZERO

    rho1 = qf.random_density(4)
    qf.bures_angle(rho, rho1)

    ket0 = qf.random_state(4)
    ket1 = qf.random_state(4)
    rho0 = ket0.asdensity()
    rho1 = ket1.asdensity()

    ang0 = qf.fubini_study_angle(ket0.vec, ket1.vec)
    ang1 = qf.bures_angle(rho0, rho1)

    assert np.isclose(ang0, ang1)
Ejemplo n.º 10
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