Exemple #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))

    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
Exemple #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))

    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