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 bity
: 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 bitx
: the first bity
: the second bit
References#
An algorithm for the T-count. Gosset et al. 2013. Figure 5.2.
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 registery
: 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)
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 bitx
: the first registery
: 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)
Counts totals:
TwoBitCSwap
: $\displaystyle n$