示例#1
0
def test_random_reduce():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1, 0)
    cgate_z = CGate((0,), Z(1))

    gate_list = [x, y, z]
    ids = list(bfs_identity_search(gate_list, 1, max_depth=4))

    circuit = (x, y, h, z, cnot)
    assert random_reduce(circuit, []) == circuit
    assert random_reduce(circuit, ids) == circuit

    seq = [2, 11, 9, 3, 5]
    circuit = (x, y, z, x, y, h)
    assert random_reduce(circuit, ids, seed=seq) == (x, y, h)

    circuit = (x, x, y, y, z, z)
    assert random_reduce(circuit, ids, seed=seq) == (x, x, y, y)

    seq = [14, 13, 0]
    assert random_reduce(circuit, ids, seed=seq) == (y, y, z, z)

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    seq = [25]
    circuit = (x, y, z, y, h, y, h, cgate_z, h, cnot)
    expected = (x, y, z, cgate_z, h, cnot)
    assert random_reduce(circuit, ids, seed=seq) == expected
    circuit = Mul(*circuit)
    assert random_reduce(circuit, ids, seed=seq) == expected
def test_random_reduce():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1, 0)
    cgate_z = CGate((0, ), Z(1))

    gate_list = [x, y, z]
    ids = list(bfs_identity_search(gate_list, 1, max_depth=4))

    circuit = (x, y, h, z, cnot)
    assert random_reduce(circuit, []) == circuit
    assert random_reduce(circuit, ids) == circuit

    seq = [2, 11, 9, 3, 5]
    circuit = (x, y, z, x, y, h)
    assert random_reduce(circuit, ids, seed=seq) == (x, y, h)

    circuit = (x, x, y, y, z, z)
    assert random_reduce(circuit, ids, seed=seq) == (x, x, y, y)

    seq = [14, 13, 0]
    assert random_reduce(circuit, ids, seed=seq) == (y, y, z, z)

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    seq = [25]
    circuit = (x, y, z, y, h, y, h, cgate_z, h, cnot)
    expected = (x, y, z, cgate_z, h, cnot)
    assert random_reduce(circuit, ids, seed=seq) == expected
    circuit = Mul(*circuit)
    assert random_reduce(circuit, ids, seed=seq) == expected
示例#3
0
def test_random_reduce():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1,0)
    cgate_z = CGate((0,), Z(1))

    seq = [2, 11, 9, 3, 5]
    gate_list = [x, y, z]
    ids = list(bfs_identity_search(gate_list, 1, max_depth=4))

    circuit = (x, y, h, z, cnot)
    assert random_reduce(circuit, []) == circuit
    assert random_reduce(circuit, ids) == circuit

    circuit = (x, y, z, x, y, h)
    # seed = 1, indices to attempt removal: 2, 11, 9, 3
    # removed id: y, z, x
    actual = random_reduce(circuit, ids, random_sequence=iter(seq))
    assert actual == (x, y, h)

    circuit = (x, x, y, y, z, z)
    # seed = 1, indices to attempt removal: 2, 11, 9
    # removed id: y, y
    actual = random_reduce(circuit, ids, random_sequence=iter(seq))
    assert actual == (x, x, z, z)

    seq = [14, 13, 0]
    # seed = 2, indices: 14, 13, 0
    # removed id: z, z
    actual = random_reduce(circuit, ids, random_sequence=iter(seq))
    assert random_reduce(circuit, ids, random_sequence=iter(seq)) == (x, x, y, y)

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    seq = [30, 29, 1, 2, 23, 19, 17, 7, 14, 13, 12, 3, 8, 7, 13, 16, 15,
           8, 6, 3]
    circuit = (x, y, z, y, h, y, h, cgate_z, h, cnot)
    expected = (x, y, z, y, h, y)
    # seed = 2, indices: 30, 29, 1, 2, 23, 19, 17, 7, 14, 13, 12, 3, 8
    #                    7, 13, 16, 15, 8, 6, 3
    # removed id: h, cgate_z, h, cnot
    actual = random_reduce(circuit, ids, random_sequence=iter(seq))
    assert actual == expected

    circuit = Mul(*(x, y, z, y, h, y, h, cgate_z, h, cnot))
    expected = (x, y, z, y, h, y)
    # seed = 2, indices: 30, 29, 1, 2, 23, 19, 17, 7, 14, 13, 12, 3, 8
    #                    7, 13, 16, 15, 8, 6, 3
    # removed id: h, cgate_z, h, cnot
    actual = random_reduce(circuit, ids, random_sequence=iter(seq))
    assert actual == expected
示例#4
0
def test_random_reduce():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1, 0)
    cgate_z = CGate((0, ), Z(1))

    seed = 1
    gate_list = [x, y, z]
    ids = list(bfs_identity_search(gate_list, 1, max_depth=4))

    circuit = (x, y, h, z, cnot)
    assert random_reduce(circuit, []) == circuit
    assert random_reduce(circuit, ids) == circuit

    circuit = (x, y, z, x, y, h)
    # seed = 1, indices to attempt removal: 2, 11, 9, 3
    # removed id: y, z, x
    actual = random_reduce(circuit, ids, seed=seed)
    assert actual == (x, y, h)

    circuit = (x, x, y, y, z, z)
    # seed = 1, indices to attempt removal: 2, 11, 9
    # removed id: y, y
    actual = random_reduce(circuit, ids, seed=seed)
    assert actual == (x, x, z, z)

    seed = 2
    # seed = 2, indices: 14, 13, 0
    # removed id: z, z
    actual = random_reduce(circuit, ids, seed=seed)
    assert random_reduce(circuit, ids, seed=seed) == (x, x, y, y)

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    circuit = (x, y, z, y, h, y, h, cgate_z, h, cnot)
    expected = (x, y, z, y, h, y)
    # seed = 2, indices: 30, 29, 1, 2, 23, 19, 17, 7, 14, 13, 12, 3, 8
    #                    7, 13, 16, 15, 8, 6, 3
    # removed id: h, cgate_z, h, cnot
    actual = random_reduce(circuit, ids, seed=seed)
    assert actual == expected

    circuit = Mul(*(x, y, z, y, h, y, h, cgate_z, h, cnot))
    expected = (x, y, z, y, h, y)
    # seed = 2, indices: 30, 29, 1, 2, 23, 19, 17, 7, 14, 13, 12, 3, 8
    #                    7, 13, 16, 15, 8, 6, 3
    # removed id: h, cgate_z, h, cnot
    actual = random_reduce(circuit, ids, seed=seed)
    assert actual == expected
示例#5
0
def test_random_insert():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1, 0)
    cgate_z = CGate((0,), Z(1))

    choices = [(x, x)]
    circuit = (y, y)
    loc, choice = 0, 0
    actual = random_insert(circuit, choices, seed=[loc, choice])
    assert actual == (x, x, y, y)

    circuit = (x, y, z, h)
    choices = [(h, h), (x, y, z)]
    expected = (x, x, y, z, y, z, h)
    loc, choice = 1, 1
    actual = random_insert(circuit, choices, seed=[loc, choice])
    assert actual == expected

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    eq_ids = flatten_ids(ids)

    circuit = (x, y, h, cnot, cgate_z)
    expected = (x, z, x, z, x, y, h, cnot, cgate_z)
    loc, choice = 1, 30
    actual = random_insert(circuit, eq_ids, seed=[loc, choice])
    assert actual == expected
    circuit = Mul(*circuit)
    actual = random_insert(circuit, eq_ids, seed=[loc, choice])
    assert actual == expected
def test_bfs_identity_search_xfail():
    scipy = import_module('scipy', __import__kwargs={'fromlist': ['sparse']})
    if scipy:
        skip("scipy installed.")
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set
示例#7
0
def test_bfs_identity_search_xfail():
    scipy = import_module('scipy', __import__kwargs={'fromlist': ['sparse']})
    if scipy:
        skip("scipy installed.")
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set
示例#8
0
def test_random_insert():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1, 0)
    cgate_z = CGate((0, ), Z(1))

    seed = 1
    choices = [(x, x)]
    circuit = (y, y)
    # insert location: 0;
    actual = random_insert(circuit, choices, seed=seed)
    assert actual == (x, x, y, y)

    seed = 8
    circuit = (x, y, z, h)
    choices = [(h, h), (x, y, z)]
    expected = (x, x, y, z, y, z, h)
    # insert location: 1; circuit choice: 1
    actual = random_insert(circuit, choices, seed=seed)
    assert actual == expected

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    collapse_eq_ids = lambda acc, an_id: acc + list(an_id.equivalent_ids)
    eq_ids = reduce(collapse_eq_ids, ids, [])

    circuit = (x, y, h, cnot, cgate_z)
    expected = (x, y, z, y, z, y, h, cnot, cgate_z)
    # insert location: 1; circuit choice: 30
    actual = random_insert(circuit, eq_ids, seed=seed)
    assert actual == expected

    circuit = Mul(*(x, y, h, cnot, cgate_z))
    expected = (x, y, z, y, z, y, h, cnot, cgate_z)
    # insert location: 1; circuit choice: 30
    actual = random_insert(circuit, eq_ids, seed=seed)
    assert actual == expected
示例#9
0
def test_random_insert():
    x = X(0)
    y = Y(0)
    z = Z(0)
    h = H(0)
    cnot = CNOT(1,0)
    cgate_z = CGate((0,), Z(1))

    seed = 1
    choices = [(x, x)]
    circuit = (y, y)
    # insert location: 0;
    actual = random_insert(circuit, choices, seed=seed)
    assert actual == (x, x, y, y)

    seed = 8
    circuit = (x, y, z, h)
    choices = [(h, h), (x, y, z)]
    expected = (x, x, y, z, y, z, h)
    # insert location: 1; circuit choice: 1
    actual = random_insert(circuit, choices, seed=seed)
    assert actual == expected

    gate_list = [x, y, z, h, cnot, cgate_z]
    ids = list(bfs_identity_search(gate_list, 2, max_depth=4))

    collapse_eq_ids = lambda acc, an_id: acc + list(an_id.equivalent_ids)
    eq_ids = reduce(collapse_eq_ids, ids, [])

    circuit = (x, y, h, cnot, cgate_z)
    expected = (x, y, z, y, z, y, h, cnot, cgate_z)
    # insert location: 1; circuit choice: 30
    actual = random_insert(circuit, eq_ids, seed=seed)
    assert actual == expected

    circuit = Mul(*(x, y, h, cnot, cgate_z))
    expected = (x, y, z, y, z, y, h, cnot, cgate_z)
    # insert location: 1; circuit choice: 30
    actual = random_insert(circuit, eq_ids, seed=seed)
    assert actual == expected
示例#10
0
def test_bfs_identity_search_xfail():
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set
示例#11
0
def test_bfs_identity_search():
    assert bfs_identity_search([], 1) == set()

    (x, y, z, h) = create_gate_sequence()

    gate_list = [x]
    id_set = {GateIdentity(x, x)}
    assert bfs_identity_search(gate_list, 1, max_depth=2) == id_set

    # Set should not contain degenerate quantum circuits
    gate_list = [x, y, z]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(x, y, z)
    }
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(y, z, y, z)
    }
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    gate_list = [x, y, z, h]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(x, h, z, h),
        GateIdentity(y, z, y, z),
        GateIdentity(y, h, y, h)
    }
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h)
    }
    assert id_set == bfs_identity_search(gate_list,
                                         1,
                                         max_depth=3,
                                         identity_only=True)

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, y, z),
        GateIdentity(x, y, x, y),
        GateIdentity(x, z, x, z),
        GateIdentity(x, h, z, h),
        GateIdentity(y, z, y, z),
        GateIdentity(y, h, y, h),
        GateIdentity(x, y, h, x, h),
        GateIdentity(x, z, h, y, h),
        GateIdentity(y, z, h, z, h)
    }
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    id_set = {
        GateIdentity(x, x),
        GateIdentity(y, y),
        GateIdentity(z, z),
        GateIdentity(h, h),
        GateIdentity(x, h, z, h)
    }
    assert id_set == bfs_identity_search(gate_list,
                                         1,
                                         max_depth=4,
                                         identity_only=True)

    cnot = CNOT(1, 0)
    gate_list = [x, cnot]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(cnot, cnot),
        GateIdentity(x, cnot, x, cnot)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_x = CGate((1, ), x)
    gate_list = [x, cgate_x]
    id_set = {
        GateIdentity(x, x),
        GateIdentity(cgate_x, cgate_x),
        GateIdentity(x, cgate_x, x, cgate_x)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_z = CGate((0, ), Z(1))
    gate_list = [cnot, cgate_z, h]
    id_set = {
        GateIdentity(h, h),
        GateIdentity(cgate_z, cgate_z),
        GateIdentity(cnot, cnot),
        GateIdentity(cnot, h, cgate_z, h)
    }
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [s, t]
    id_set = {GateIdentity(s, s, s, s)}
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set
示例#12
0
def test_bfs_identity_search():
    assert bfs_identity_search([], 1) == set()

    (x, y, z, h) = create_gate_sequence()

    gate_list = [x]
    id_set = set([GateIdentity(x, x)])
    assert bfs_identity_search(gate_list, 1, max_depth=2) == id_set

    # Set should not contain degenerate quantum circuits
    gate_list = [x, y, z]
    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(x, y, z)])
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(x, y, z),
                  GateIdentity(x, y, x, y),
                  GateIdentity(x, z, x, z),
                  GateIdentity(y, z, y, z)])
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    gate_list = [x, y, z, h]
    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(h, h),
                  GateIdentity(x, y, z),
                  GateIdentity(x, y, x, y),
                  GateIdentity(x, z, x, z),
                  GateIdentity(x, h, z, h),
                  GateIdentity(y, z, y, z),
                  GateIdentity(y, h, y, h)])
    assert bfs_identity_search(gate_list, 1) == id_set

    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(h, h)])
    assert id_set == bfs_identity_search(gate_list, 1, max_depth=3,
                                         identity_only=True)

    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(h, h),
                  GateIdentity(x, y, z),
                  GateIdentity(x, y, x, y),
                  GateIdentity(x, z, x, z),
                  GateIdentity(x, h, z, h),
                  GateIdentity(y, z, y, z),
                  GateIdentity(y, h, y, h),
                  GateIdentity(x, y, h, x, h),
                  GateIdentity(x, z, h, y, h),
                  GateIdentity(y, z, h, z, h)])
    assert bfs_identity_search(gate_list, 1, max_depth=5) == id_set

    id_set = set([GateIdentity(x, x),
                  GateIdentity(y, y),
                  GateIdentity(z, z),
                  GateIdentity(h, h),
                  GateIdentity(x, h, z, h)])
    assert id_set == bfs_identity_search(gate_list, 1, max_depth=4,
                                         identity_only=True)

    cnot = CNOT(1, 0)
    gate_list = [x, cnot]
    id_set = set([GateIdentity(x, x),
                  GateIdentity(cnot, cnot),
                  GateIdentity(x, cnot, x, cnot)])
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_x = CGate((1,), x)
    gate_list = [x, cgate_x]
    id_set = set([GateIdentity(x, x),
                  GateIdentity(cgate_x, cgate_x),
                  GateIdentity(x, cgate_x, x, cgate_x)])
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    cgate_z = CGate((0,), Z(1))
    gate_list = [cnot, cgate_z, h]
    id_set = set([GateIdentity(h, h),
                  GateIdentity(cgate_z, cgate_z),
                  GateIdentity(cnot, cnot),
                  GateIdentity(cnot, h, cgate_z, h)])
    assert bfs_identity_search(gate_list, 2, max_depth=4) == id_set

    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [s, t]
    id_set = set([GateIdentity(s, s, s, s)])
    assert bfs_identity_search(gate_list, 1, max_depth=4) == id_set
示例#13
0
def test_bfs_identity_search_xfail():
    s = PhaseGate(0)
    t = TGate(0)
    gate_list = [Dagger(s), t]
    id_set = {GateIdentity(Dagger(s), t, t)}
    assert bfs_identity_search(gate_list, 1, max_depth=3) == id_set