# How to convert Tableau to Circuit in stim

I am trying to simulate random Clifford circuit. I can use stim.Tableau.random(n) to generate it, but don't know how to convert into stim.Circuit() form. It seems current v1.9 does not have to_circuit() function.

This is not yet available in stim v1.9, but stim v1.10 will have stim.Tableau.to_circuit. You can install the latest in-development version (as of this post) via pip install stim==1.10.dev1659567261 and then you can do:

import stim
tableau = stim.Tableau.random(5)
print(tableau)
print(tableau.to_circuit(method="elimination"))

+-xz-xz-xz-xz-xz-
| -+ +- +- ++ +-
| __ XX ZY YZ ZY
| _Z XX Z_ YZ _Y
| _Y XZ __ YY __
| X_ XX _Z YY YX
| XY __ _Y ZY YX

CX 2 0 0 2 2 0
H 0
S 0 3 4
CX 0 3 0 4
S 1 4
H 1 4
CX 1 0 3 0 4 0 4 1 1 4 4 1 1 2 3 1 4 1 4 2 2 4 4 2
H 2
S 2
CX 2 3 2 4
H 3 4
CX 3 2 4 2 4 3 3 4 4 3
H 3 4
CX 3 4
S 4
H 4
S 4
H 0 1 3
S 0 0 1 1 3 3
H 0 1 3
S 1 1 4 4


Which is:

-X-@-X-H-S-@-@-----X-X-X---------------------------------------------H-S-S-H-----
| | |     | |     | | |
-|-|-|-----|-|-S-H-@-|-|-X-@-X-@-X-X---------------------------------H-S-S-H-S-S-
| | |     | |       | | | | | | | |
-@-X-@-----|-|-------|-|-|-|-|-X-|-|-X-@-X-H-S-@-@---X-X-------------------------
| |       | | | | |   | | | | |     | |   | |
---------S-X-|-------@-|-|-|-|---@-|-|-|-|-----X-|-H-@-|-X-@-X-H-@---H-S-S-H-----
|         | | | |     | | | |       |     | | | |   |
---------S---X-S-H-----@-@-X-@-----@-@-X-@-------X-H---@-@-X-@-H-X-S-H-S-S---S---


As you can see, this method is going out of its way to not use anything exception H, S, CX and also it is not attempting to make a shallow circuit.

• Thanks for the answer! By the way, this there any way to define a gate(so that I can append this random gate directly)? Or append circuits on specific qubits(in this case, the "gate circuit" has a smaller size than the full circuit)? Aug 6 at 5:22
• @YuchengHe Stim has no concept of defining a custom gate for a circuit. There's also no built-in method for transforming which qubits the circuit operates on. You'll have to write that yourself by iterating over the operations and transforming them individually. Aug 6 at 5:39