Blocks System
Blocks are the basic component of a quantum circuit in Yao.
Block System
The whole framework is consist of a block system. The whole system characterize a quantum circuit into serveral kinds of blocks. The uppermost abstract type for the whole system is AbstractBlock
Composite Blocks
Roller
Roller
is a special pattern of quantum circuits. Usually is equivalent to a KronBlock
, but we can optimize the computation by rotate the tensor form of a quantum state and apply each small block on it each time.
Blocks
Yao.Blocks.AbstractBlock
— Type.AbstractBlock
abstract type that all block will subtype from. N
is the number of qubits.
Required interfaces * apply!
or (and) mat
Interfaces for parametric blocks.
* iparameters
* setiparameters
Yao.Blocks.AbstractContainer
— Type.ContainerBlock{N, T} <: MatrixBlock{N, T}
abstract supertype which container blocks will inherit from.
extended APIs
block
: the block contained by this ContainerBlock
Yao.Blocks.AbstractMeasure
— Type.AbstractMeasure <: AbstractBlock
Abstract block supertype which measurement block will inherit from.
Yao.Blocks.AbstractScale
— Type.AbstractScale{N, T} <: TagBlock{N, T}
Block for scaling siblings by a factor of X.
Yao.Blocks.AddBlock
— Type.AddBlock{N, T} <: CompositeBlock{N, T}
Adding multiple blocks into one.
Yao.Blocks.BPDiff
— Type.BPDiff{GT, N, T, PT} <: AbstractDiff{GT, N, Complex{T}}
BPDiff(block, [grad]) -> BPDiff
Mark a block as differentiable, here GT
, PT
is gate type, parameter type.
Warning: please don't use the adjoint
after BPDiff
! adjoint
is reserved for special purpose! (back propagation)
Yao.Blocks.BlockTreeIterator
— Type.BlockTreeIterator{BT}
Iterate through the whole block tree with breadth first search.
Yao.Blocks.CacheFragment
— Type.CacheFragment{BT, K, MT}
A fragment that will be stored for each cached block (of type BT
) on a cache server.
Yao.Blocks.CachedBlock
— Type.Yao.Blocks.ChainBlock
— Type.ChainBlock{N, T} <: CompositeBlock{N, T}
ChainBlock
is a basic construct tool to create user defined blocks horizontically. It is a Vector
like composite type.
Yao.Blocks.CompositeBlock
— Type.CompositeBlock{N, T} <: MatrixBlock{N, T}
abstract supertype which composite blocks will inherit from.
extended APIs
blocks
: get an iteratable of all blocks contained by this CompositeBlock
Yao.Blocks.Concentrator
— Type.Concentrator{N, T, BT <: AbstractBlock} <: AbstractContainer{N, T}
concentrates serveral lines together in the circuit, and expose it to other blocks.
Yao.Blocks.ConstantGate
— Type.ConstantGate{N, T} <: PrimitiveBlock{N, T}
Abstract type for constant gates.
Yao.Blocks.ControlBlock
— Type.ControlBlock{N, BT<:AbstractBlock, C, M, T} <: AbstractContainer{N, T}
N: number of qubits, BT: controlled block type, C: number of control bits, T: type of matrix.
Yao.Blocks.Daggered
— Type.Daggered{N, T, BT} <: TagBlock{N, T}
Daggered(blk::BT)
Daggered{N, T, BT}(blk)
Daggered Block.
Yao.Blocks.FunctionBlock
— Type.FunctionBlock <: AbstractBlock
This block contains a general function that perform an in-place operation over a register
Yao.Blocks.KronBlock
— Type.KronBlock{N, T, MT<:MatrixBlock} <: CompositeBlock{N, T}
composite block that combine blocks by kronecker product.
Yao.Blocks.MathBlock
— Type.MathBlock{L, N, T} <: PrimitiveBlock{N, T}
Block for arithmatic operations, the operation name can be specified by type parameter L. Note the T
parameter represents the kind of view of basis (the input format of func
), which should be one of bint
, bint_r
, bfloat
, bfloat_r
.
Yao.Blocks.MatrixBlock
— Type.MatrixBlock{N, T} <: AbstractBlock
abstract type that all block with a matrix form will subtype from.
Yao.Blocks.Measure
— Type.Measure <: AbstractMeasure
Measure() -> Measure
Measure block, collapse a state and store measured value, e.g.
Examples
julia> m = Measure();
julia> reg = product_state(4, 7)
DefaultRegister{1, Complex{Float64}}
active qubits: 4/4
julia> reg |> m
DefaultRegister{1, Complex{Float64}}
active qubits: 4/4
julia> m.result
1-element Array{Int64,1}:
7
Note: Measure
returns a vector here, the length corresponds to batch dimension of registers.
Yao.Blocks.MeasureAndRemove
— Type.MeasureAndRemove <: AbstractMeasure
MeasureAndRemove() -> MeasureAndRemove
Measure and remove block, remove measured qubits and store measured value.
Yao.Blocks.MeasureAndReset
— Type.MeasureAndReset <: AbstractMeasure
MeasureAndReset([val=0]) -> MeasureAndReset
Measure and reset block, reset measured qubits to val
and store measured value.
Yao.Blocks.PhaseGate
— Type.PhiGate
Global phase gate.
Yao.Blocks.PrimitiveBlock
— Type.PrimitiveBlock{N, T} <: MatrixBlock{N, T}
abstract type that all primitive block will subtype from. A primitive block is a concrete block who can not be decomposed into other blocks. All composite block can be decomposed into several primitive blocks.
NOTE: subtype for primitive block with parameter should implement hash
and ==
method to enable key value cache.
Yao.Blocks.PutBlock
— Type.PutBlock{N, C, GT, T} <: AbstractContainer{N, T}
put a block on given addrs.
Yao.Blocks.QDiff
— Type.QDiff{GT, N, T} <: AbstractDiff{GT, N, Complex{T}}
QDiff(block) -> QDiff
Mark a block as quantum differentiable.
Yao.Blocks.ReflectBlock
— Type.ReflectBlock{N, T} <: PrimitiveBlock{N, T}
Householder reflection with respect to some target state, $|\psi\rangle = 2|s\rangle\langle s|-1$.
Yao.Blocks.RepeatedBlock
— Type.RepeatedBlock{N, C, GT, T} <: AbstractContainer{N, T}
repeat the same block on given addrs.
Yao.Blocks.Roller
— Type.Roller{N, T, BT} <: CompositeBlock{N, T}
map a block type to all lines and use a rolling method to evaluate them.
TODO
fill identity like KronBlock
-> To interface.
Yao.Blocks.RotationGate
— Type.RotationGate{N, T, GT <: MatrixBlock{N, Complex{T}}} <: PrimitiveBlock{N, Complex{T}}
RotationGate, with GT both hermitian and isreflexive.
Yao.Blocks.Scale
— Type.Scale{BT, FT, N, T} <: AbstractScale{N, T}
Scale(block, factor) -> Scale
Scale Block.
Yao.Blocks.Sequential
— Type.Sequential <: AbstractBlock
sequencial structure that looser than a chain, it does not require qubit consistency and does not have mat
method.
Yao.Blocks.ShiftGate
— Type.ShiftGate <: PrimitiveBlock
Phase shift gate.
Yao.Blocks.StaticScale
— Type.StaticScale{X, BT, N, T} <: AbstractScale{N, T}
StaticScale{X}(blk::MatrixBlock)
StaticScale{X, N, T, BT}(blk::MatrixBlock)
Scale Block, by a static factor of X, notice X is static!
Yao.Blocks.TagBlock
— Type.TagBlock{N, T} <: AbstractContainer{N, T}
TagBlock is a special kind of Container, it is a size keeper.
Yao.Blocks.TimeEvolution
— Type.TimeEvolution{N, TT, GT} <: PrimitiveBlock{N, ComplexF64}
TimeEvolution(H::GT, t::TT; tol::Real=1e-7) -> TimeEvolution
TimeEvolution, where GT is block type. input matrix should be hermitian.
Yao.Blocks.apply!
— Function.apply!(reg, block, [signal])
apply a block
to a register reg
with or without a cache signal.
Yao.Blocks.applymatrix
— Method.applymatrix(g::AbstractBlock) -> Matrix
Transform the apply! function of specific block to dense matrix.
Yao.Blocks.backward!
— Method.backward!(δ::AbstractRegister, circuit::MatrixBlock) -> AbstractRegister
back propagate and calculate the gradient ∂f/∂θ = 2Re(∂f/∂ψ⋅∂ψ/∂θ), given ∂f/∂ψ.
Note: Here, the input circuit should be a matrix block, otherwise the back propagate may not apply (like Measure operations).
Yao.Blocks.block
— Function.block(container::AbstractContainer) -> AbstractBlock
get the contained block (i.e. subblock) of a container.
Yao.Blocks.blockfilter
— Method.blockfilter(func, blk::AbstractBlock) -> Vector{AbstractBlock}
blockfilter!(func, rgs::Vector, blk::AbstractBlock) -> Vector{AbstractBlock}
tree wise filtering for blocks.
Yao.Blocks.chblock
— Function.chblock(block, blk)
change the block of a container.
Yao.Blocks.chfactor
— Function.chfactor(blk::AbstractScale) -> AbstractScale
change scaling factor of blk
.
Yao.Blocks.chsubblocks
— Method.chsubblocks(pb::AbstractBlock, blks) -> AbstractBlock
Change subblocks
of target block.
Yao.Blocks.dispatch!!
— Method.dispatch!!([func::Function], block::AbstractBlock, params) -> AbstractBlock
Similar to dispatch!
, but will pop! out params inplace, it can not more efficient.
Yao.Blocks.dispatch!
— Method.dispatch!([func::Function], block::AbstractBlock, params) -> AbstractBlock
dispatch!([func::Function], block::AbstractBlock, :random) -> AbstractBlock
dispatch!([func::Function], block::AbstractBlock, :zero) -> AbstractBlock
dispatch! parameters into this circuit, here params
is an iterable.
If instead of iterable, a symbol :random
or :zero
is provided, random numbers (its behavior is specified by setiparameters!
) or 0s will be broadcasted into circuits.
using dispatch!!
is more efficient, but will pop! out all params inplace.
Yao.Blocks.expect
— Function.expect(op::AbstractBlock, reg::AbstractRegister{B}) -> Vector
expect(op::AbstractBlock, dm::DensityMatrix{B}) -> Vector
expectation value of an operator.
Yao.Blocks.factor
— Function.factor(blk::AbstractScale) -> Number
get scaling factor of blk
.
Yao.Blocks.generator
— Method.generator(rot::Rotor) -> MatrixBlock
Return the generator of rotation block.
Yao.Blocks.gradient
— Function.gradient(circuit::AbstractBlock, mode::Symbol=:ANY) -> Vector
collect all gradients in a circuit, mode can be :BP/:QC/:ANY, they will collect grad
from BPDiff/QDiff/AbstractDiff respectively.
Yao.Blocks.iparameter_type
— Method.iparameter_type(block::AbstractBlock) -> Type
element type of iparameters(block)
.
Yao.Blocks.iparameters
— Function.iparameters(block) -> Vector
Returns a list of all intrinsic (not from sublocks) parameters in block.
Yao.Blocks.mat
— Function.mat(block) -> Matrix
Returns the matrix form of this block.
Yao.Blocks.niparameters
— Function.niparameters(x) -> Integer
Returns the number of parameters of x
.
Yao.Blocks.nparameters
— Method.nparameters(c::AbstractBlock) -> Int
number of parameters, including parameters in sublocks.
Yao.Blocks.parameter_type
— Function.parameter_type(block) -> Type
the type of iparameters.
Yao.Blocks.parameters
— Function.parameters(c::AbstractBlock, [output]) -> Vector
get all parameters including sublocks.
Yao.Blocks.setiparameters!
— Function.setparameters!([elementwisefunction], r::AbstractBlock, params::Number...) -> AbstractBlock
setparameters!([elementwisefunction], r::AbstractBlock, :random) -> AbstractBlock
setparameters!([elementwisefunction], r::AbstractBlock, :zero) -> AbstractBlock
set intrinsics parameter for block, input params
can be numbers or :random or :zero.
Yao.Blocks.subblocks
— Function.subblocks(blk::AbstractBlock) -> Tuple
return a tuple of all sub-blocks in this block.
Yao.Blocks.traverse
— Method.traverse(blk; algorithm=:DFS) -> BlockTreeIterator
Returns an iterator that traverse through the block tree.
Yao.Blocks.usedbits
— Method.addrs(block::AbstractBlock) -> Vector{Int}
Occupied addresses (include control bits and bits occupied by blocks), fall back to all bits if this method is not provided.
Yao.Intrinsics.isreflexive
— Function.isreflexive(x) -> Bool
Test whether this operator is reflexive.
Yao.Intrinsics.isunitary
— Function.isunitary(x) -> Bool
Test whether this operator is unitary.
Yao.Registers.datatype
— Method.datatype(x) -> DataType
Returns the data type of x.
Yao.nqubits
— Method.nqubits(::Type{MT}) -> Int
nqubits(::MatrixBlock) -> Int
Return the number of qubits of a MatrixBlock
.
Base.collect
— Method.collect(circuit::AbstractBlock, ::Type{BT}) where BT<:AbstractBlock
collect blocks of type BT
in the block tree with circuit
as root.
Yao.Blocks._allmatblock
— Method.all blocks are matrix blocks
Yao.Blocks._blockpromote
— Method.promote types of blocks
Yao.Blocks.cache_key
— Function.cache_key(block)
Returns the key that identify the matrix cache of this block. By default, we use the returns of parameters
as its key.
Yao.Blocks.cache_type
— Method.cache_type(::Type) -> DataType
A type trait that defines the element type that a CacheFragment
will use.
Yao.Blocks.istraitkeeper
— Function.istraitkeeper(block) -> Bool
change the block of a container.
Yao.Blocks.print_block
— Method.print_block(io, block)
define the style to print this block
Yao.Blocks.render_params
— Method.render_params(r::AbstractBlock, raw_parameters) -> Iterable
More elegant way of rendering parameters for symbols.
@const_gate NAME = MAT_EXPR
@const_gate NAME::Type = MAT_EXPR
@const_Gate NAME::Type
This macro simplify the definition of a constant gate. It will automatically bind the matrix form to a constant which will reduce memory allocation in the runtime.
@const_gate X = ComplexF64[0 1;1 0]
or
@const_gate X::ComplexF64 = [0 1;1 0]
You can bind new element types by simply re-declare with a type annotation.
@const_gate X::ComplexF32