Exemple #1
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
Exemple #2
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
Exemple #3
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