1
$\begingroup$

I would like to know if there is a functionality in qiskit to simplify a sequence of gates in a QASM file. For example, say we have a sequence of two expensive (in fault-tolerant context) gates $TT$. Instead of executing an operation $TT$, it is preferable to re-express $TT$ with a single Clifford gate $S$, i.e., $TT=S$.

Is there a qiskit function that allows the mapping $TT \rightarrow S$ in a QASM file? Also, sequences like $HH$ etc could be simplified as well.

$\endgroup$

2 Answers 2

2
$\begingroup$

If your issue is just the mapping $TT \rightarrow S$, you could try the following:

$-$ STEP 1: manually add the gates equivalence $TT = S$ to the qiskit.circuit.equivalence_library.SessionEquivalenceLibrary (it is not included by default)

from qiskit import QuantumCircuit
from qiskit.circuit.equivalence_library import SessionEquivalenceLibrary as sel

tt_qc = QuantumCircuit(1)
tt_qc.t(0)
tt_qc.t(0)
tt_gate = tt_qc.to_gate()

s_qc = QuantumCircuit(1)
s_qc.s(0)

sel.add_equivalence(gate=tt_gate, equivalent_circuit=s_qc)

$-$ STEP 2: build your circuit using the tt_gate previously defined

qc = QuantumCircuit(1)
qc.append(tt_gate, [0])
qc.decompose().draw('mpl')

enter image description here

$-$ STEP 3: transpile your circuit passing the $S$ gate as the basis_gates parameter

from qiskit import transpile

qc = transpile(qc, basis_gates=['s'])
qc.draw('mpl')

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks a lot Simone. In Step 2, do I have to create the circuit with the tt_gate or it works for any circuit which has two Ts in a sequence? Of course, the latter is the general case that I need. $\endgroup$
    – MonteNero
    Dec 15, 2022 at 22:46
1
$\begingroup$

The following function should do the job correctly. It takes your QASM file as an argument and it returns a Python str object containing your "simplified" (or better transpiled) QASM code:

from qiskit import QuantumCircuit
from qiskit import transpile

def get_simplified_qasm(qasm_file):
    qc = QuantumCircuit.from_qasm_file(qasm_file)
    qc = transpile(qc, optimization_level=2)
    return qc.qasm()

As an example, suppose you have the following circ.qasm file:

OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
h q[0];
h q[0];

To automatically simplify your circuit so that $HH \rightarrow I$, you can just call the get_simplified_qasm function:

new_qasm = get_simplified_qasm('circ.qasm')
print(new_qasm)

The result, as expected, will be the following:

OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
$\endgroup$
2
  • $\begingroup$ PS: notice that this will not work for $TT \rightarrow S$ but this happens just because, in general, it's not true that implementing two T gates is more "expensive" than a single S gate but it really depends on the backend (simulator or real quantum device) you are using to run your program. On the other hand, $HH \rightarrow I$ always works because applying two $H$ gates is for sure more "expensive" than doing nothing ($I$) $\endgroup$ Dec 15, 2022 at 19:14
  • $\begingroup$ Thanks for the reply. Having $TT$ mapped to $S$ is the most important functionality. Implementing non-Clifford gates (such as T) in a fault-tolerant manner is very costly. That's why I need $TT \rightarrow S$ mapping. $\endgroup$
    – MonteNero
    Dec 15, 2022 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.