Beispiel #1
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)
Beispiel #2
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
def test_parameter_shift_circuits() -> None:
    """Checks that gradients calculated with middle out algorithm
    match gradients calculated from parameter shift rule.
    """
    graph = nx.grid_graph([2, 2])
    layers = 2
    params = qf.graph_circuit_params(graph, layers)
    circ = qf.graph_circuit(graph, layers, params)
    N = circ.qubit_nb
    qubits = circ.qubits

    ket0 = qf.zero_state(qubits)
    ket1 = qf.random_state(qubits)
    grads = qf.state_fidelity_gradients(ket0, ket1, circ)

    for n in range(N):
        r, circ0, circ1 = qf.parameter_shift_circuits(circ, n)
        fid0 = qf.state_fidelity(circ0.run(ket0), ket1)
        fid1 = qf.state_fidelity(circ1.run(ket0), ket1)
        grad = r * (fid1 - fid0)
    assert np.isclose(grad, grads[n])
Beispiel #4
0
def test_swap_test():
    import examples.swaptest as ex
    ket0 = qf.zero_state([0])
    ket1 = qf.random_state([1])
    ket2 = qf.random_state([2])

    ket = qf.join_states(ket0, ket1)
    ket = qf.join_states(ket, ket2)

    fid = qf.state_fidelity(ket1, ket2.relabel([1]))
    st_fid = ex.swap_test(ket, 0, 1, 2)

    assert qf.asarray(fid) / qf.asarray(st_fid) == ALMOST_ONE
def test_swap_test():
    import examples.swaptest as ex

    ket0 = qf.zero_state([0])
    ket1 = qf.random_state([1])
    ket2 = qf.random_state([2])

    ket = qf.join_states(ket0, ket1)
    ket = qf.join_states(ket, ket2)

    fid = qf.state_fidelity(ket1, ket2.on(1))
    st_fid = ex.swap_test(ket, 0, 1, 2)

    assert np.isclose(fid, st_fid)
Beispiel #6
0
    def main():
        """CLI"""
        print(swap_test.__doc__)

        print('Randomly generating two 1-qubit states...')

        ket0 = qf.zero_state([0])
        ket1 = qf.random_state([1])
        ket2 = qf.random_state([2])

        ket = qf.join_states(ket0, ket1, ket2)

        fid = qf.state_fidelity(ket1, ket2.relabel([1]))
        st_fid = swap_test(ket, 0, 1, 2)

        print('Fidelity:               ', qf.asarray(fid))
        print('Fidelity from swap test:', qf.asarray(st_fid))
Beispiel #7
0
    def main():
        """CLI"""
        print(swap_test.__doc__)

        print("Randomly generating two 1-qubit states...")

        ket0 = qf.zero_state([0])
        ket1 = qf.random_state([1])
        ket2 = qf.random_state([2])

        ket = qf.join_states(ket0, ket1)
        ket = qf.join_states(ket, ket2)

        fid = qf.state_fidelity(ket1, ket2.on(1))
        st_fid = swap_test(ket, 0, 1, 2)

        print("Fidelity:               ", fid)
        print("Fidelity from swap test:", st_fid)