-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new general control * add itercontrol * new PutBlock * new put block, more general control * benchmark crot toffoli * benchmark crot toffoli * updatebenchmarks * new h benchmark * new benchmark results * statify a sparse array * fix test failures * new rotbasis * new general matrix gate * add rotbasis & fix parameters * fix rotbasis
- Loading branch information
Showing
24 changed files
with
736 additions
and
345 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export GeneralMatrixGate | ||
|
||
mutable struct GeneralMatrixGate{M, N, T} <: PrimitiveBlock{N, T} | ||
matrix :: AbstractMatrix{T} | ||
function GeneralMatrixGate{M, N, T}(matrix::AbstractMatrix{T}) where {M, N, T} | ||
(1<<M == size(matrix, 1) && 1<<N == size(matrix, 2)) || throw(DimensionMismatch("Dimension of input matrix shape error.")) | ||
new{M, N, T}(matrix) | ||
end | ||
end | ||
GeneralMatrixGate(matrix::AbstractMatrix{T}) where T = GeneralMatrixGate{log2i(size(matrix, 1)), log2i(size(matrix, 2)), T}(matrix) | ||
|
||
==(A::GeneralMatrixGate, B::GeneralMatrixGate) = A.matrix == B.matrix | ||
copy(r::GeneralMatrixGate) = GeneralMatrixGate(copy(r.matrix)) | ||
|
||
mat(r::GeneralMatrixGate) = r.matrix | ||
|
||
function print_block(io::IO, g::GeneralMatrixGate{M, N, T}) where {M,N,T} | ||
print("GeneralMatrixGate(2^$M × 2^$N)") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,57 @@ | ||
@static if VERSION >= v"0.7-" | ||
using FFTW | ||
end | ||
|
||
export QFTCircuit, QFTBlock, breflect, invorder_firstdim | ||
|
||
CRk(i::Int, j::Int, k::Int) = control([i, ], j=>shift(-2π/(1<<k))) | ||
CRot(n::Int, i::Int) = chain(i==j ? put(i=>H) : CRk(j, i, j-i+1) for j = i:n) | ||
QFT(n::Int) = chain(n, CRot(n, i) for i = 1:n) | ||
CRot(n::Int, i::Int) = chain(i==j ? kron(i=>H) : CRk(j, i, j-i+1) for j = i:n) | ||
QFTCircuit(n::Int) = chain(n, CRot(n, i) for i = 1:n) | ||
|
||
struct QFTBlock{N} <: PrimitiveBlock{N,ComplexF64} end | ||
mat(q::QFTBlock{N}) where N = applymatrix(q) | ||
|
||
apply!(reg::DefaultRegister{B}, ::QFTBlock) where B = (reg.state = fft!(invorder_firstdim(reg |> state), 1)/sqrt(1<<nqubits(reg)); reg) | ||
apply!(reg::DefaultRegister{B}, ::Daggered{N, T, <:QFTBlock}) where {B,N,T} = (reg.state = invorder_firstdim(ifft!(reg|>state, 1)*sqrt(1<<nqubits(reg))); reg) | ||
|
||
# traits | ||
ishermitian(q::QFTBlock{N}) where N = N==1 | ||
isreflexive(q::QFTBlock{N}) where N = N==1 | ||
isunitary(q::QFTBlock{N}) where N = true | ||
|
||
function breflect(num_bit::Int, b::Int) | ||
for i in 1:num_bit÷2 | ||
b = swapbits(b, bmask(i, num_bit-i+1)) | ||
end | ||
b | ||
end | ||
|
||
function breflect(num_bit::Int, b::Int, mask::Vector{Int}) | ||
@simd for m in mask | ||
b = swapbits(b, m) | ||
end | ||
b | ||
end | ||
|
||
function invorder_firstdim(v::Matrix) | ||
w = similar(v) | ||
n = size(v, 1) |> log2i | ||
n_2 = n ÷ 2 | ||
mask = [bmask(i, n-i+1) for i in 1:n_2] | ||
@simd for b in basis(n) | ||
@inbounds w[breflect(n, b, mask)+1,:] = v[b+1,:] | ||
end | ||
w | ||
end | ||
|
||
function invorder_firstdim(v::Vector) | ||
n = length(v) |> log2i | ||
n_2 = n ÷ 2 | ||
w = similar(v) | ||
#mask = SVector{n_2, Int}([bmask(i, n-i+1)::Int for i in 1:n_2]) | ||
mask = [bmask(i, n-i+1)::Int for i in 1:n_2] | ||
@simd for b in basis(n) | ||
@inbounds w[breflect(n, b, mask)+1] = v[b+1] | ||
end | ||
w | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
export RotBasis, randpolar, polar2u, u2polar, rot_basis | ||
|
||
""" | ||
RotBasis{T} <: PrimitiveBlock{1, Complex{T}} | ||
A special rotation block that transform basis to angle θ and ϕ in bloch sphere. | ||
""" | ||
mutable struct RotBasis{T} <: PrimitiveBlock{1, Complex{T}} | ||
theta::T | ||
phi::T | ||
end | ||
|
||
# chain -> * | ||
# mat(rb::RotBasis{T}) where T = mat(Ry(-rb.theta))*mat(Rz(-rb.phi)) | ||
function mat(x::RotBasis{T}) where T | ||
R1 = _make_rot_mat(IMatrix{2, Complex{T}}(), mat(Z), -x.phi) | ||
R2 = _make_rot_mat(IMatrix{2, Complex{T}}(), mat(Y), -x.theta) | ||
R2 * R1 | ||
end | ||
|
||
==(rb1::RotBasis, rb2::RotBasis) = rb1.theta == rb2.theta && rb1.phi == rb2.phi | ||
|
||
copy(block::RotBasis{T}) where T = RotBasis{T}(block.theta, block.phi) | ||
dispatch!(block::RotBasis, params) = ((block.theta, block.phi) = params; block) | ||
|
||
parameters(rb::RotBasis) = (rb.theta, rb.phi) | ||
nparameters(::Type{<:RotBasis}) = 2 | ||
|
||
function print_block(io::IO, R::RotBasis) | ||
print(io, "RotBasis($(R.theta), $(R.phi))") | ||
end | ||
|
||
function hash(gate::RotBasis, h::UInt) | ||
hash(hash(gate.theta, gate.phi, objectid(gate)), h) | ||
end | ||
|
||
rot_basis(num_bit::Int) = dispatch!(chain(num_bit, put(i=>RotBasis(0.0, 0.0)) for i=1:num_bit), randpolar(num_bit) |> vec) | ||
|
||
""" | ||
u2polar(vec::Array) -> Array | ||
transform su(2) state vector to polar angle, apply to the first dimension of size 2. | ||
""" | ||
function u2polar(vec::Vector) | ||
ratio = vec[2]/vec[1] | ||
[atan(abs(ratio))*2, angle(ratio)] | ||
end | ||
|
||
""" | ||
polar2u(vec::Array) -> Array | ||
transform polar angle to su(2) state vector, apply to the first dimension of size 2. | ||
""" | ||
function polar2u(polar::Vector) | ||
theta, phi = polar | ||
[cos(theta/2)*exp(-im*phi/2), sin(theta/2)*exp(im*phi/2)] | ||
end | ||
|
||
u2polar(arr::Array) = mapslices(u2polar, arr, [1]) | ||
polar2u(arr::Array) = mapslices(polar2u, arr, [1]) | ||
|
||
""" | ||
randpolar(params::Int...) -> Array | ||
random polar basis, number of basis | ||
""" | ||
randpolar(params::Int...) = rand(2, params...)*pi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
module Zoo | ||
using Compat | ||
|
||
using ..Yao | ||
using ..Blocks | ||
using ..LuxurySparse | ||
using ..Intrinsics | ||
using ..Registers | ||
using ..Blocks | ||
import ..Blocks: mat, dispatch!, nparameters, parameters, cache_key, print_block, _make_rot_mat, apply! | ||
import Base: ==, copy, hash | ||
import ..Intrinsics: ishermitian, isreflexive, isunitary | ||
|
||
# Block APIs | ||
export QFT | ||
export diff_circuit, num_gradient, rotter, cnot_entangler, opgrad, collect_rotblocks | ||
|
||
include("QFT.jl") | ||
include("Differential.jl") | ||
include("RotBasis.jl") | ||
include("Grover.jl") | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Compat | ||
using Compat.Test | ||
|
||
using Yao | ||
using Yao.Blocks | ||
|
||
import Yao.Blocks: GeneralMatrixGate | ||
|
||
@testset "MatrixGate" begin | ||
mg = GeneralMatrixGate(randn(4,4)) | ||
mg2 = copy(mg) | ||
@test mg2 == mg | ||
mg2.matrix[:,2] = 10 | ||
@test mg2 != mg | ||
@test nqubits(mg) == 2 | ||
@test_throws DimensionMismatch GeneralMatrixGate(randn(3,3)) | ||
|
||
reg = rand_state(2) | ||
@test copy(reg) |> mg |> statevec == mg.matrix * reg.state |> vec | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.