Example #1
0
    elif l in G_2:
        k = model[space[l]].as_long()
        print("gate {} is at cycle {} on edge ({},{})".format(l, model[time[l]], e[k][0], e[k][1]))
        [q0, q1] = g[l]
        q0 = model[pi[q0][model[time[l]].as_long()]].as_long()
        q1 = model[pi[q1][model[time[l]].as_long()]].as_long()
        scheduled[model[time[l]].as_long()].append("{} q[{}], q[{}];\n".format(gate_spec[l], q0, q1))
for m in range(M):
    print("logical qubit {} is mapped to node {} in the beginning, node {} at the end".format(m, model[pi[m][0]], model[pi[m][result_depth - 1]]))
for k in range(K):
    for t in range(max_time + 1):
        if model[sigma[k][t]]:
            print("A swap gate finished at cycle {} on edge ({}, {}).".format(t, e[k][0], e[k][1]))
            scheduled[t].append("cx q[{}], q[{}]; // SWAP on edge {} at transition {}\n".format(e[k][0], e[k][1], k, t))
            scheduled[t].append("cx q[{}], q[{}]; // SWAP on edge {} at transition {}\n".format(e[k][1], e[k][0], k, t))
            scheduled[t].append("cx q[{}], q[{}]; // SWAP on edge {} at transition {}\n".format(e[k][0], e[k][1], k, t))

outFile = open(new_file_name + '.qasm', 'w')
outFile.write("// optimized for device {}\n".format(device_name))
outFile.writelines(['\n', "OPENQASM 2.0;\n", "include \"qelib1.inc\";\n", "qreg q[{}];\n".format(N), "creg c[{}];\n".format(M)])
for t in range(max_time + 1):
    outFile.writelines(['\n', "// cycle {}\n".format(t)])
    outFile.writelines(scheduled[t])
# outFile.writelines(['\n', "measure q -> c;\n"])
for m in range(M):
    outFile.write("measure q[{}] -> c[{}];\n".format(model[pi[m][max_time - 1]], m))
outFile.close()


qiskit_info_after(new_file_name + '.qasm', new_file_name, original_cx_count)
Example #2
0
from qiskit import QuantumCircuit
import sys


def tket_run(file_name, device_name):
    connection_list = qcdevice(device_name).connection_list
    circ = circuit_from_qasm(file_name)
    circ.measure_all()
    arc = Architecture(connection_list)
    dev = Device(arc)
    routed_circ = route(circ, arc)
    # cu = CompilationUnit(routed_circ)
    Transform.DecomposeBRIDGE().apply(routed_circ)
    # pass1 = DecomposeSwapsToCXs(dev)
    # pass1.apply(cu)
    # circ2 = cu.circuit
    return routed_circ


file_name = sys.argv[1]
device_name = sys.argv[2]
new_file_name = "result/paper/" + file_name.split('/')[-1].replace(
    '.qasm', "_{}_tket".format(device_name))

original_cx_count = qiskit_info_original(file_name, new_file_name)[1]

routed_circ = tket_run(file_name, device_name)
# circuit_to_qasm(circ, "result/paper/" + file_name.split('/')[-1] + "_tket.qasm")
circ = tk_to_qiskit(routed_circ)
qiskit_info_after(circ, new_file_name, original_cx_count)