Example #1
0
def test_convert_to_from_pyzx_optimizing_circuit(tequila_circuit, t_reduce):

    pyzx_circuit = convert_to_pyzx(tequila_circuit)

    pyzx_graph = pyzx_circuit.to_graph()

    if t_reduce:
        pyzx.teleport_reduce(pyzx_graph)
        pyzx_circuit_opt = pyzx.Circuit.from_graph(pyzx_graph)
    else:
        pyzx.full_reduce(pyzx_graph)
        pyzx_graph.normalize()
        pyzx_circuit_opt = pyzx.extract_circuit(pyzx_graph.copy())

    # compare_tensors returns True if pyzx_circuit and pyzx_circuit_opt
    # implement the same circuit (up to global phase)
    assert (pyzx.compare_tensors(pyzx_circuit, pyzx_circuit_opt))

    # verify_equality return True if full_reduce() is able to reduce the
    # composition of the circuits to the identity
    assert (pyzx_circuit.verify_equality(pyzx_circuit_opt))

    converted_circuit = convert_from_pyzx(pyzx_circuit_opt)

    wfn1 = simulate(tequila_circuit, backend="symbolic")
    wfn2 = simulate(converted_circuit, backend="symbolic")

    assert (numpy.isclose(wfn1.inner(wfn2), 1.0))
def teleport_reduce(c, g):

    orig_tcount = c.tcount()
    orig_2qubitcount = c.twoqubitcount()

    zx.teleport_reduce(g)
    try:
        c_opt = zx.Circuit.from_graph(g)
    except:
        c_opt = zx.extract_circuit(g.copy())

    opt_tcount = c_opt.tcount()
    opt_2qubitcount = c_opt.twoqubitcount()
    if orig_tcount == opt_tcount and orig_2qubitcount == opt_2qubitcount:
        return False, (c, g)
    return True, (c_opt, g)
Example #3
0
 def _optimize(self, c):
     g = c.to_graph()
     zx.teleport_reduce(g)
     c_opt = zx.Circuit.from_graph(g)
     return c_opt
Example #4
0
    # zx.draw(c)
    plt.show()
    plt.close('all')

    g = c.to_graph()

    g_fr = g.copy()
    zx.full_reduce(g_fr)
    c_fr = zx.extract_circuit(g_fr.copy()).to_basic_gates()
    c_fr = zx.basic_optimization(c_fr)
    # note: we don't reset g_fr here because for any future annealing, we'd really optimize after the graph produced by full_reduce, rather than something resulting from extraction
    print("\n----- full_reduce + basic_optimization -----")
    print(c_fr.stats())

    g_just_tr = g.copy()
    zx.teleport_reduce(g_just_tr)
    c_just_tr = zx.Circuit.from_graph(g_just_tr.copy()).to_basic_gates()
    print("\n----- teleport_reduce -----")
    print(c_just_tr.stats())

    g_tr = g.copy()
    zx.teleport_reduce(g_tr)
    c_tr = zx.Circuit.from_graph(g_tr.copy()).to_basic_gates()
    # c_opt = zx.full_optimize(c_opt)
    c_tr = zx.basic_optimization(c_tr)
    g_tr = c_tr.to_graph()
    print("\n----- teleport_reduce + basic_optimization -----")
    print(c_tr.stats())
    to_graph_like(g_tr)

    g_tr_extract = g.copy()
                                                             depth=n_qubits * gates_per_qubit,
                                                             clifford=False))

    N_ITERS = 5000
    INTERVAL = 250
    improvement_after = { x: list() for x in range(0, N_ITERS, INTERVAL) }

    REDUCE_METHOD = "TR"

    for c in tqdm(cs, desc="Circuits..."):
        g = c.to_graph()

        g_tmp = g.copy()

        if REDUCE_METHOD == "TR":
            zx.teleport_reduce(g_tmp)
            c_tr = zx.Circuit.from_graph(g_tmp.copy()).to_basic_gates()
            c_tr = zx.basic_optimization(c_tr)
            g_simp = c_tr.to_graph()
        elif REDUCE_METHOD == "FR":
            zx.full_reduce(g_tmp)
            g_simp = g_tmp.copy()
        else:
            raise RuntimeError(f"Invalid REDUCE_METHOD: {REDUCE_METHOD}")

        to_graph_like(g_simp)

        _, scores = pivot_anneal(g_simp, iters=N_ITERS)
        final_score = scores[-1]
        for x in range(0, N_ITERS, INTERVAL):
            if final_score < scores[x]:
Example #6
0
    plt.tight_layout()
    plt.show()
    """

    # Compare GA vs SA

    N_QUBITS = 9
    DEPTH = 100

    c = zx.generate.CNOT_HAD_PHASE_circuit(qubits=N_QUBITS,
                                           depth=DEPTH,
                                           clifford=False)
    g = c.to_graph()

    g_tr = g.copy()
    zx.teleport_reduce(g_tr)
    c_tr = zx.Circuit.from_graph(g_tr.copy())
    c_tr = zx.basic_optimization(c_tr).to_basic_gates()
    g_tr = c_tr.to_graph()
    to_graph_like(g_tr)

    # fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
    fig, axes = plt.subplots(nrows=1, ncols=2)

    ## GA part

    N_MUTANTS = 100
    N_GENS = 100
    actions = [ga.rand_pivot, ga.rand_lc, ga.do_nothing]
    # actions = [rand_lc, rand_pivot]
    ga_opt = ga.GeneticOptimizer(actions,