gray_synth_toffoli#
- gray_synth_toffoli(qc: QuantumCircuit) QuantumCircuit[source]#
Replace Toffoli gates with a gray-synthesis decomposition.
Qrispβs default Toffoli implementation is optimised for T-depth, since the majority of Qrisp algorithms target fault-tolerant execution where T gates dominate cost. The default decomposition does not have a higher CNOT count than the gray-synthesis variant, but it requires more SWAP gates when routed onto linear nearest-neighbour connectivity.
The gray-synthesis decomposition uses 6 CNOT gates and does not inherently satisfy linear connectivity either, but the router consistently resolves the gap with a single SWAP whose constituent CNOTs partially cancel with a Toffoli CNOT β resulting in 7 physical CNOTs and a displaced target qubit.
- Parameters:
- qcQuantumCircuit
The input quantum circuit.
- Returns:
- QuantumCircuit
A new circuit with every Toffoli replaced by the gray-synthesis decomposition.
Examples
We showcase the distinction in Toffoli decompositions.
>>> from qrisp import QuantumCircuit, PassManager >>> from qrisp import gray_synth_toffoli, decompose >>> qc = QuantumCircuit(3) >>> qc.ccx(0, 1, 2) >>> print(qc) qb_95: βββ ββ β qb_96: βββ ββ βββ΄ββ qb_97: β€ X β βββββ
>>> pm_0 = PassManager() >>> pm_0 += decompose() >>> decomposed_qc = pm_0.run(qc) >>> print(decomposed_qc) βββββββ qb_95: β€ Tdg βββββββββ ββββββββββ βββββ ββββββββββββββββββββββββ ββ βββββββ€βββββ β ββββββββ΄ββ β ββββββββββββ βββββ βββ΄ββ qb_96: β€ Tdg ββ€ X ββββΌβββ€ T ββ€ X ββββΌβββ€ Tdg ββ€ X βββ€ T βββ€ X β ββ¬ββββ¬ββββ¬βββββ΄βββββββ€ββββββββ΄ββββββββββββ¬ββββ΄ββββ΄ββββββ€ qb_97: ββ€ H βββββ βββ€ X ββ€ T βββββββ€ X βββββββββββ βββ€ Tdg ββ€ H β βββββ ββββββββββ βββββ ββββββββββββ
While this implementation has only a T-depth of 4, the CX gates essentially βcycleβ through the connectivity requirements. On a linear chain connectivity, several swaps would be required.
>>> pm_1 = PassManager() >>> pm_1 += gray_synth_toffoli >>> pm_1 += decompose() >>> optimized_qc = pm_1.run(qc) >>> print(optimized_qc) βββββ ββββββββββ qb_95: β€ T βββββββββββββββββββββ ββββββββββββββββββββββ βββββ ββββ€ gphase ββββ ββ βββββ€ β β βββ΄ββββ΄βββββββββ€βββ΄ββ qb_96: β€ T βββββββββ ββββββββββββΌββββββββββ ββββββββββββΌβββ€ X ββ€ P(-Ο/4) ββ€ X β βββββ€ββββββββ΄ββββββββββββ΄ββββββββββ΄ββββββββββββ΄βββββββ€ββββββββββββββββ qb_97: β€ H ββ€ T ββ€ X ββ€ Tdg ββ€ X ββ€ T ββ€ X ββ€ Tdg ββ€ X ββ€ H βββββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
This implementation has T-depth 5 but the first 4 CX gates can be implemented swap-free on a linear chain connectivity. After this, a single SWAP (that can be fused with one of the CX) is suffificient to execute the remaining CX.