# How to use qiskit transpile with Clifford + T basis? [duplicate]

How can I transpile using universal $$Clifford + T$$ gates set? I have only seen examples using rotations + $$CNOT$$.

This is what I have tried

from qiskit import *
from qiskit.quantum_info import Operator
from qiskit.compiler import transpile
%matplotlib inline

u = Operator([[0, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0]])

qc = QuantumCircuit(3)
qc.unitary(u, [0,1,2], label='u')

result = transpile(qc, basis_gates=['u1', 'u2', 'u3', 'cx'], optimization_level=3)
result.draw(output='mpl')


And this example works fine. But when I was trying to set:

basis_gates=['h', 's', 't', 'cx']


It doesn't work. Could you help me, please?)

UPDATE: After comments I've tried this:

pip install git+https://github.com/LNoorl/qiskit-terra.git@feature/sk-pass


and then:

from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import TGate, HGate, TdgGate, SGate
from qiskit.transpiler.passes import SolovayKitaevDecomposition
from qiskit import *
from qiskit.quantum_info import Operator
from qiskit.compiler import transpile
%matplotlib inline

u = Operator([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, -1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]])

qc = QuantumCircuit(3)
qc.unitary(u, [0,1,2], label='u')

print('Orginal circuit:')
print(qc)

basis_gates = [TGate(), SGate(), HGate()]
skd = SolovayKitaevDecomposition(recursion_degree=2, basis_gates=basis_gates, depth=5)

discretized = skd(qc)

print('Discretized circuit:')
print(discretized)


But it outputs only this:

Orginal circuit:
┌────┐
q_0: ┤0   ├
│    │
q_1: ┤1 u ├
│    │
q_2: ┤2   ├
└────┘
Discretized circuit:
┌────┐
q_0: ┤0   ├
│    │
q_1: ┤1 u ├
│    │
q_2: ┤2   ├
└────┘


Where is a problem?

• I need to use H, S, T, CNOT gates. Apr 23 at 12:40
• You can decompose your circuit in CNOT and single qubit gates with transpile(circuit, basis_gates=['u', 'cx']) and then you just need to find a Clifford+T approximation, which is discussed in the question @luciano referred to. Since Clifford includes S and H that gives you the H, S, T and CNOT basis. Apr 23 at 12:43
• I have updated the question) Apr 23 at 12:52
• Try the decompose method on the circuit, something like qc.decompose().draw() would show you the inside of your black box
– Lena
Apr 23 at 13:00
• It shows me  ┌───────────┐ q_0: ┤0 ├ │ │ q_1: ┤1 ISOMETRY ├ │ │ q_2: ┤2 ├ └───────────┘  Apr 23 at 13:04

The reason for your second result is because unitary synthesis is not part of the SolovayKitaevDecomposition pass. Actually, you can combine your attempts to get the desired result.

This is the first stage (you first snippet), it will synthesise your circuit in terms of u* and cx:

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.compiler import transpile

u = Operator([[0, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0, 0, 0]])

qc = QuantumCircuit(3)
qc.unitary(u, [0,1,2], label='u')

transpiled = transpile(qc, basis_gates=['u1', 'u2', 'u3', 'cx'], optimization_level=3)


Then you can transform it to Cliffort+T using SolovayKitaevDecomposition:

from qiskit.circuit.library import TGate, HGate, SGate
from qiskit.transpiler.passes import SolovayKitaevDecomposition

basis_gates = [TGate(), SGate(), HGate()]
skd = SolovayKitaevDecomposition(recursion_degree=3, basis_gates=basis_gates, depth=5)
discretized = skd(transpiled)
print(discretized)

global phase: -0.063526
┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐                           »
q_0: ┤ X ├┤ T ├┤ X ├┤ S ├┤ X ├┤ X ├┤ S ├┤ S ├┤ H ├───────────────────────────»
└─┬─┘└───┘└─┬─┘└───┘└─┬─┘└─┬─┘├───┤├───┤├───┤┌───┐┌───┐┌───┐┌───┐┌─────┐»
q_1: ──■─────────┼─────────■────┼──┤ X ├┤ S ├┤ S ├┤ S ├┤ X ├┤ S ├┤ H ├┤ TDG ├»
│              │  └─┬─┘└───┘└───┘└───┘└─┬─┘├───┤├───┤└┬───┬┘»
q_2: ────────────■──────────────■────■───────────────────■──┤ T ├┤ S ├─┤ S ├─»
└───┘└───┘ └───┘ »