예제 #1
0
def test_channel_angle() -> None:
    chan0 = qf.X(0).aschannel()
    chan1 = qf.Y(0).aschannel()
    qf.channel_angle(chan0, chan1)

    assert not qf.channels_close(chan0, chan1)
    assert qf.channels_close(chan0, chan0)
예제 #2
0
def test_chan_permute():
    chan0 = qf.CNOT(0, 1).aschannel()
    chan1 = qf.CNOT(1, 0).aschannel()

    assert not qf.channels_close(chan0, chan1)

    chan2 = chan1.permute([0, 1])
    assert chan2.qubits == (0, 1)
    assert qf.channels_close(chan1, chan2)

    chan3 = chan1.relabel([0, 1])
    assert qf.channels_close(chan0, chan3)
예제 #3
0
def test_channel_adjoint():
    kraus0 = qf.Damping(0.1, 0)
    chan0 = kraus0.aschannel()
    chan1 = chan0.H.H
    assert qf.channels_close(chan0, chan1)

    chan2 = kraus0.H.aschannel()
    assert qf.channels_close(chan2, chan0.H)

    # 2 qubit hermitian channel
    chan3 = qf.CZ(0, 1).aschannel()
    chan4 = chan3.H
    assert qf.channels_close(chan3, chan4)
예제 #4
0
def test_chan_permute() -> None:
    chan0 = qf.CNot(0, 1).aschannel()
    chan1 = qf.CNot(1, 0).aschannel()

    assert not qf.channels_close(chan0, chan1)

    chan2 = chan1.permute([0, 1])
    assert chan2.qubits == (0, 1)
    assert qf.channels_close(chan1, chan2)

    chan3 = chan1.on(0, 1)
    print(chan0.qubits, chan3.qubits)
    assert qf.channels_close(chan0, chan3)
예제 #5
0
    def _roundtrip(kraus):
        assert qf.kraus_iscomplete(kraus)

        chan0 = kraus.aschannel()
        kraus1 = qf.channel_to_kraus(chan0)
        assert qf.kraus_iscomplete(kraus1)

        chan1 = kraus1.aschannel()
        assert qf.channels_close(chan0, chan1)
예제 #6
0
def test_multiswapgate() -> None:
    # Should be same as a swap.
    perm0 = qf.MultiSwapGate([0, 1], [1, 0])
    gate0 = qf.Swap(0, 1)
    assert qf.gates_close(perm0.asgate(), gate0)
    assert qf.gates_close(perm0.asgate(), perm0.H.asgate())

    perm1 = qf.MultiSwapGate.from_gates(qf.Circuit([gate0]))
    assert qf.gates_close(perm0.asgate(), perm1.asgate())

    perm2 = qf.MultiSwapGate.from_gates(qf.Circuit([perm1]))
    assert qf.gates_close(perm0, perm2)

    with pytest.raises(ValueError):
        qf.MultiSwapGate.from_gates(qf.Circuit(qf.CNot(0, 1)))

    N = 8
    qubits_in = list(range(N))
    qubits_out = np.random.permutation(qubits_in)

    permN = qf.MultiSwapGate(qubits_in, qubits_out)
    assert qf.gates_close(perm0.asgate(), perm1.asgate())
    iden = qf.Circuit([permN, permN.H])
    assert qf.almost_identity(iden.asgate())
    assert qf.circuits_close(iden, qf.Circuit([qf.IdentityGate(qubits_in)]))

    swaps = qf.Circuit(permN.decompose())
    # Add identity so we don't lose qubits
    swaps += qf.IdentityGate(permN.qubits_in)
    permN2 = qf.MultiSwapGate.from_gates(swaps)

    assert qf.circuits_close(swaps, qf.Circuit([permN]))
    assert qf.circuits_close(swaps, qf.Circuit([permN2]))
    assert qf.circuits_close(qf.Circuit([permN]), qf.Circuit([permN2]))

    with pytest.raises(ValueError):
        _ = qf.MultiSwapGate([0, 1], [1, 2])

    # Channels
    assert qf.channels_close(perm0.aschannel(), gate0.aschannel())

    rho0 = qf.random_state([0, 1, 3]).asdensity()
    rho1 = perm0.evolve(rho0)
    rho2 = gate0.aschannel().evolve(rho0)
    assert qf.densities_close(rho1, rho2)
예제 #7
0
def test_partial_trace():
    data = [1] * (2**16)

    r8 = qf.QubitVector(data, range(2))
    r4 = qf.QubitVector(data, range(4))
    r2 = qf.QubitVector(data, range(8))

    tr2 = r2.partial_trace([1])
    assert tr2.qubits == (0, 2, 3, 4, 5, 6, 7)
    assert tr2.rank == 2

    tr2 = r2.partial_trace([2, 3])
    assert tr2.qubits == (0, 1, 4, 5, 6, 7)
    assert tr2.rank == 2

    tr4 = r4.partial_trace([0])
    assert tr4.qubits == (1, 2, 3)
    assert tr4.rank == 4

    tr8 = r8.partial_trace([1])
    assert tr8.qubits == (0, )
    assert tr8.rank == 8

    with pytest.raises(ValueError):
        r2.partial_trace(range(8))

    chan012 = qf.identity_gate(3).aschannel()
    assert np.isclose(qf.asarray(chan012.trace()), 64)  # 2**(2**3)

    chan02 = chan012.partial_trace([1])
    assert np.isclose(qf.asarray(chan02.trace()), 32)  # TODO: Checkme
    chan2 = chan012.partial_trace([0, 1])

    assert np.isclose(qf.asarray(chan2.trace()), 16)  # TODO: checkme

    # partial traced channels should be identities still, upto normalization
    assert qf.channels_close(chan2, qf.I(2).aschannel())
    # TODO: Channel.normalize()

    with pytest.raises(ValueError):
        qf.zero_state(4).vec.partial_trace([1, 2])
예제 #8
0
def test_channle_choi() -> None:
    chan0 = qf.Damping(0.1, 0).aschannel()
    choi = chan0.choi()
    chan1 = qf.Channel.from_choi(choi, [0])
    assert qf.channels_close(chan0, chan1)