Пример #1
0
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 test_canonical_decomposition() -> None:
    for tt1 in range(0, 6):
        for tt2 in range(tt1):
            for tt3 in range(tt2):
                t1, t2, t3 = tt1 / 12, tt2 / 12, tt3 / 12
                if t3 == 0 and t1 > 0.5:
                    continue
                coords = np.asarray((t1, t2, t3))

                circ0 = qf.Circuit()
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                circ0 += qf.Can(t1, t2, t3, 0, 1)
                circ0 += qf.RandomGate([0])
                circ0 += qf.RandomGate([1])
                gate0 = circ0.asgate()

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

                canon = circ1[1]
                new_coords = np.asarray(
                    [canon.param(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))
Пример #3
0
def test_decomp_stdgates():
    gate0 = qf.I(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.CNOT(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.SWAP(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.ISWAP(0, 1)
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.CNOT(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.SWAP(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate0 = qf.ISWAP(0, 1)**0.5
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)
Пример #4
0
def test_decomp_stdgates() -> None:
    gate0 = qf.IdentityGate([0, 1])
    gate1 = qf.canonical_decomposition(gate0).asgate()
    assert qf.gates_close(gate0, gate1)

    gate2 = qf.CNot(0, 1)
    gate3 = qf.canonical_decomposition(gate2).asgate()
    assert qf.gates_close(gate2, gate3)

    gate4 = qf.Swap(0, 1)
    gate5 = qf.canonical_decomposition(gate4).asgate()
    assert qf.gates_close(gate4, gate5)

    gate6 = qf.ISwap(0, 1)
    gate7 = qf.canonical_decomposition(gate6).asgate()
    assert qf.gates_close(gate6, gate7)

    gate8 = qf.CNot(0, 1)**0.5
    gate9 = qf.canonical_decomposition(gate8).asgate()
    assert qf.gates_close(gate8, gate9)

    gate10 = qf.Swap(0, 1)**0.5
    gate11 = qf.canonical_decomposition(gate10).asgate()
    assert qf.gates_close(gate10, gate11)

    gate12 = qf.ISwap(0, 1)**0.5
    gate13 = qf.canonical_decomposition(gate12).asgate()
    assert qf.gates_close(gate12, gate13)
Пример #5
0
def test_can_to_cnot() -> None:
    gate = qf.Can(0.3, 0.23, 0.22, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    assert qf.gates_close(gate, circ.asgate())

    gate = qf.Can(0.3, 0.23, 0.0, 0, 1)
    circ = qf.Circuit(qf.translate_can_to_cnot(gate))  # type: ignore
    print(qf.canonical_decomposition(circ.asgate()))
    assert qf.gates_close(gate, circ.asgate())
Пример #6
0
def test_decomp_sqrtswap_sandwich():
    circ0 = qf.Circuit()
    circ0 += qf.CANONICAL(1 / 4, 1 / 4, 1 / 4, 0, 1)
    circ0 += qf.random_gate([0])
    circ0 += qf.random_gate([1])
    circ0 += qf.CANONICAL(1 / 4, 1 / 4, 1 / 4, 0, 1)

    gate0 = circ0.asgate()
    circ1 = qf.canonical_decomposition(gate0)
    gate1 = circ1.asgate()
    assert qf.gates_close(gate0, gate1)
Пример #7
0
def test_decomp_sqrtswap_sandwich() -> None:
    circ0 = qf.Circuit()
    circ0 += qf.Can(1 / 4, 1 / 4, 1 / 4, 0, 1)
    circ0 += qf.RandomGate([0])
    circ0 += qf.RandomGate([1])
    circ0 += qf.Can(1 / 4, 1 / 4, 1 / 4, 0, 1)

    gate0 = circ0.asgate()
    circ1 = qf.canonical_decomposition(gate0)
    gate1 = circ1.asgate()
    assert qf.gates_close(gate0, gate1)
Пример #8
0
def test_canonical_decomp_sandwich():
    for _ in range(REPS):
        # Random CZ sandwich
        circ0 = qf.Circuit()
        circ0 += qf.random_gate([0])
        circ0 += qf.random_gate([1])
        circ0 += qf.CZ(0, 1)
        circ0 += qf.TY(0.4, 0)
        circ0 += qf.TY(0.25, 1)
        circ0 += qf.CZ(0, 1)
        circ0 += qf.random_gate([0])
        circ0 += qf.random_gate([1])

        gate0 = circ0.asgate()

        circ1 = qf.canonical_decomposition(gate0)
        gate1 = circ1.asgate()

        assert qf.gates_close(gate0, gate1)
        assert qf.almost_unitary(gate0)
Пример #9
0
def test_canonical_decomp_errors():
    # Wrong number of qubits
    with pytest.raises(ValueError):
        qf.canonical_decomposition(qf.X())
Пример #10
0
def test_canonical_decomp_random():
    for _ in range(REPS * 2):
        gate0 = qf.random_gate([0, 1])
        gate1 = qf.canonical_decomposition(gate0).asgate()
        assert qf.gates_close(gate0, gate1)