Basic Swaps#

from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register
from qualtran import QBit, QInt, QUInt, QAny
from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma
from typing import *
import numpy as np
import sympy
import cirq

TwoBitSwap#

Swap two bits.

This is a Clifford operation.

Registers#

  • x: the first bit

  • y: the second bit

from qualtran.bloqs.basic_gates import TwoBitSwap

Example Instances#

swap_bit = TwoBitSwap()

Graphical Signature#

from qualtran.drawing import show_bloqs
show_bloqs([swap_bit],
           ['`swap_bit`'])

TwoBitCSwap#

Swap two bits controlled on a control bit.

This is sometimes known as the Fredkin Gate.

Registers#

  • ctrl: the control bit

  • x: the first bit

  • y: the second bit

References#

from qualtran.bloqs.basic_gates import TwoBitCSwap

Example Instances#

cswap_bit = TwoBitCSwap()

Graphical Signature#

from qualtran.drawing import show_bloqs
show_bloqs([cswap_bit],
           ['`cswap_bit`'])

Clifford+T circuit#

In Qualtran, this bloq is treated as atomic because its implementation is architecture-dependent. A clifford+T compilation is provided via a Cirq circuit on the to_clifford_t_circuit() method

cswap_bit.to_clifford_t_circuit()
                            ┌──┐          ┌─────┐
ctrl: ───────@───T───────────@─────@───────────@────────────────
             │               │     │           │
x: ──────X───X───T^-1───X────┼T────X───────T^-1┼────X───T───X───
         │              │    │                 │    │       │
y: ──────@───H───T──────@────X─────T^-1────────X────@───H───@───
                            └──┘          └─────┘

Swap#

Swap two registers

This corresponds to a qubitwise TwoBitSwap on the two registers.

Parameters#

  • bitsize: The bitsize of each of the two registers being swapped.

Registers#

  • x: the first register

  • y: the second register

from qualtran.bloqs.basic_gates import Swap

Example Instances#

n = sympy.Symbol('n', positive=True, integer=True)
swap = Swap(bitsize=n)
swap_small = Swap(bitsize=4)
swap_large = Swap(bitsize=64)

Graphical Signature#

from qualtran.drawing import show_bloqs
show_bloqs([swap, swap_small, swap_large],
           ['`swap`', '`swap_small`', '`swap_large`'])

Call Graph#

from qualtran.resource_counting.generalizers import ignore_split_join
swap_g, swap_sigma = swap.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(swap_g)
show_counts_sigma(swap_sigma)
../../_images/9514e505bcc5ff7a78fe2144cb5ca166159b33572bddf29f23d4b8c9499bb162.svg

Counts totals:

  • TwoBitSwap: $\displaystyle n$

CSwap#

Swap two registers controlled on a control bit.

This decomposes into a qubitwise TwoBitCSwap on the two target registers, and takes \(n\) TwoBitCSwap gates.

Parameters#

  • bitsize: The bitsize of each of the two registers being swapped.

Registers#

  • ctrl: the control bit

  • x: the first register

  • y: the second register

from qualtran.bloqs.basic_gates import CSwap

Example Instances#

n = sympy.Symbol('n', positive=True, integer=True)
cswap = CSwap(bitsize=n)
# A small version on four bits.
cswap_small = CSwap(bitsize=4)
# A large version that swaps 64-bit registers.
cswap_large = CSwap(bitsize=64)

Graphical Signature#

from qualtran.drawing import show_bloqs
show_bloqs([cswap, cswap_small, cswap_large],
           ['`cswap`', '`cswap_small`', '`cswap_large`'])

Call Graph#

from qualtran.resource_counting.generalizers import ignore_split_join
cswap_g, cswap_sigma = cswap.call_graph(max_depth=1, generalizer=ignore_split_join)
show_call_graph(cswap_g)
show_counts_sigma(cswap_sigma)
../../_images/cba56644c0065a9d0657cfb6a6aa1085a597969fba721b81b20425f602db1d12.svg

Counts totals:

  • TwoBitCSwap: $\displaystyle n$