Repository navigation
Add a Power block to yaoblocks #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 21 commits
472d37a
107b2a1
3588d6d
247bd55
e52ef80
423e4fb
31b4d4f
95fdbe6
e76b506
ea59fa7
d8662f2
8406a46
d33538b
984d894
74f3647
5caacac
754c0ee
b846a36
c488cc2
4aaef2f
7e11795
19401bf
e434889
a4195c8
727acd9
e72953e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,23 @@ function mat_back!(::Type{T}, rb::KronBlock, adjy, collector) where {T} | |
| return collector | ||
| end | ||
|
|
||
| function mat_back!(::Type{T}, rb::Power{D, BT, PT}, adjy, collector) where {T, D, BT<:AbstractBlock, PT<:Integer} | ||
| nparameters(rb) == 0 && return collector | ||
|
harry-phasecraft marked this conversation as resolved.
Resolved
|
||
| blk = content(rb) | ||
| adjy = rb.pow > 0 ? adjy : adjy' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure this is correct, AFAICT above.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wouldnt disagree except that i couldnt get that to pass the tests and this did. The implemtation for Daggered also does this step
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GiggleLiu or @Roger-luo can maybe one of you weigh in here? We are both a little bit confused as to how |
||
| U = mat(T, blk) | ||
| U_adj = U' | ||
| n = abs(rb.pow) | ||
| adjb = adjy * (U_adj)^(n-1) | ||
| adjc = adjb | ||
| for _ in 1:n-1 | ||
| adjb = U_adj * adjb * U | ||
| adjc += adjb | ||
| end | ||
| mat_back!(T, blk, adjc, collector) | ||
| return collector | ||
| end | ||
|
|
||
| function mat_back!(::Type{T}, rb::AbstractAdd, adjy, collector) where {T} | ||
| nparameters(rb) == 0 && return collector | ||
| for b in subblocks(rb)[end:-1:1] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| export Power | ||
|
|
||
| using LinearAlgebra | ||
| using LegibleLambdas | ||
|
|
||
| """ | ||
| Power{D,GT<:AbstractBlock,PT<:Real} <: AbstractContainer{GT,D} | ||
| Power(block, pow) -> Power | ||
|
|
||
| Repeat the same block `content` `pow` times | ||
| """ | ||
| struct Power{D,BT<:AbstractBlock,PT<:Integer} <: AbstractContainer{BT,D} | ||
| content::BT | ||
| pow::PT | ||
| end | ||
|
|
||
| function Power(content::BT, pow::PT) where {D,BT<:AbstractBlock{D},PT} | ||
| if pow isa Int && pow < 0 && !isunitary(content) | ||
|
harry-phasecraft marked this conversation as resolved.
Resolved
|
||
| throw(ArgumentError("negative power requires a unitary block")) | ||
| end | ||
| Power{D,BT,PT}(content, pow) | ||
| end | ||
| Power(content::LegibleLambda, pow::PT) where {PT} = @λ(n -> Power(content(n), pow)) | ||
|
|
||
| nqudits(pb::Power) = nqudits(pb.content) | ||
| chsubblocks(pb::Power, blk::AbstractBlock) = Power(blk, pb.pow) | ||
| occupied_locs(pb::Power) = occupied_locs(pb.content) | ||
|
|
||
| function mat(::Type{T}, pb::Power{D}) where {T, D} | ||
| pb.pow == 0 && return IMatrix{T}(D^nqudits(pb.content)) | ||
| pb.pow > 0 && return mat(T, pb.content)^pb.pow | ||
| return mat(T, adjoint(pb.content))^(-pb.pow) # unitary: U^(-n) = (U†)^n | ||
| end | ||
|
|
||
| function YaoAPI.unsafe_apply!(r::AbstractRegister, pb::Power{D}) where {D} | ||
| blk = pb.pow >= 0 ? pb.content : adjoint(pb.content) | ||
| for _ in 1:abs(pb.pow) | ||
| YaoAPI.unsafe_apply!(r, blk) | ||
| end | ||
| return r | ||
| end | ||
|
|
||
| function nparameters(pb::Power) | ||
| return iszero(pb.pow) ? 0 : nparameters(pb.content) | ||
| end | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I am not sure of the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me the problem is that if pow is 0 then there are no parameters regardless of what the content is which means. Is this because it will mess with how you might think
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We definitely want that And conceptually I guess the thing is that
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the suggested changes now. Will need QuantumBFS/LuxurySparse.jl#82 though before it fully works. |
||
| Base.adjoint(pb::Power) = Power(adjoint(pb.content), pb.pow) | ||
| Base.:(==)(a::Power, b::Power) = a.pow == b.pow && a.content == b.content | ||
| Base.copy(pb::Power) = Power(pb.content, pb.pow) | ||
| cache_key(pb::Power) = (pb.pow, cache_key(pb.content)) | ||
| PropertyTrait(::Power) = PreserveAll() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,16 @@ function eigenbasis(op::ChainBlock) | |
| end | ||
| end | ||
|
|
||
| function eigenbasis(op::Power) | ||
| b = op.pow >= 0 ? content(op) : adjoint(content(op)) | ||
| E = chain(b.n) | ||
| blks = chain(b.n) | ||
| Ei, Vi = eigenbasis(b) | ||
| push!(E, fill(Ei, abs(op.pow))...) | ||
| push!(blks, fill(Vi, abs(op.pow))...) | ||
| return E, blks | ||
| end | ||
|
Comment on lines
+58
to
+61
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The eigenvectors of function eigenbasis(op::Power)
E, V = eigenbasis(content(op))
return E ^ op.pow, V
endwhere you need to check the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is solved now. |
||
|
|
||
| function eigenbasis(op::Add) | ||
| # detect commute operators | ||
| if simple_commute_eachother(subblocks(op)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| using Test, YaoBlocks | ||
| using LinearAlgebra | ||
|
|
||
| # Multi-qubit circuits to use as Power content | ||
| circ2 = chain(2, put(1 => Rx(0.3)), put(2 => Rz(0.7)), cnot(2, 1, 2)) | ||
| circ3 = chain(3, put(1 => H), kron(3, 1 => Rx(0.2), 2 => Ry(0.4)), put(3 => T)) | ||
|
|
||
| @testset "Power" begin | ||
| pb = X ^ 3 | ||
| @test pb isa Power | ||
| @test pb == Power(X, 3) | ||
|
|
||
| pb = put(2, 1=>X)^3 | ||
| println(pb) | ||
| @test YaoBlocks.color(Power) == :yellow | ||
| pb2 = copy(pb) | ||
| @test pb2 == pb | ||
| pb2 = put(2, 1=>X)^2 | ||
| @test cache_key(pb) != cache_key(pb2) | ||
| @test YaoBlocks.PropertyTrait(pb) == YaoBlocks.PreserveAll() | ||
| @test occupied_locs(pb) == (1,) | ||
| end | ||
|
|
||
| @testset "mat correctness" begin | ||
| for (gate, nq_gate) in [(X, 1), (Rx(0.3), 1), (circ2, 2), (circ3, 3)] | ||
| for n in -2:3 | ||
| pb = gate^n | ||
| # Use Matrix() to avoid sparse-type issues with ^0 | ||
| @test Matrix(mat(pb)) ≈ Matrix(mat(gate))^n | ||
| @test Matrix(mat(pb')) ≈ Matrix(mat(gate'))^n | ||
| @test Matrix(mat(pb')) ≈ Matrix(mat(pb))' | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "apply! on random state" begin | ||
| for (gate, nq_gate) in [(X, 1), (Rx(0.3), 1), (circ2, 2), (circ3, 3)] | ||
| for n in 0:3 | ||
| pb = gate^n | ||
| reg = rand_state(nq_gate) | ||
| reg_ref = copy(reg) | ||
| for _ in 1:n | ||
| apply!(reg_ref, gate) | ||
| end | ||
| apply!(reg, pb) | ||
| @test statevec(reg) ≈ statevec(reg_ref) | ||
|
|
||
| # adjoint | ||
| reg2 = rand_state(nq_gate) | ||
| reg2_ref = copy(reg2) | ||
| for _ in 1:n | ||
| apply!(reg2_ref, gate') | ||
| end | ||
| apply!(reg2, pb') | ||
| @test statevec(reg2) ≈ statevec(reg2_ref) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "negative powers" begin | ||
| # Negative power requires unitary content | ||
| @test_throws ArgumentError matblock(rand(ComplexF64, 2, 2))^(-1) | ||
|
|
||
| for (gate, nq_gate) in [(X, 1), (Rx(0.3), 1), (circ2, 2), (circ3, 3)] | ||
| for n in -3:-1 | ||
| pb = gate^n | ||
| # apply!: should be the same as applying adjoint |n| times | ||
| reg = rand_state(nq_gate) | ||
| reg_ref = copy(reg) | ||
| for _ in 1:abs(n) | ||
| apply!(reg_ref, gate') | ||
| end | ||
| apply!(reg, pb) | ||
| @test statevec(reg) ≈ statevec(reg_ref) | ||
| # adjoint of a negative-power block | ||
| @test Matrix(mat(pb')) ≈ Matrix(mat(pb))' | ||
| end | ||
| end | ||
| end | ||
|
|
||
| nq = 4 | ||
| @testset "inside put block" begin | ||
| for (gate, nq_gate) in [(X, 1), (Rx(0.3), 1), (circ2, 2), (circ3, 3)] | ||
| locs = Tuple(1:nq_gate) | ||
| for n in [0, 1, 2, 3] | ||
| pb = gate^n | ||
| circuit = put(nq, locs => pb) | ||
| reg = rand_state(nq) | ||
| reg_ref = copy(reg) | ||
| for _ in 1:n | ||
| apply!(reg_ref, put(nq, locs => gate)) | ||
| end | ||
| apply!(reg, circuit) | ||
| @test statevec(reg) ≈ statevec(reg_ref) | ||
| @test Matrix(mat(circuit)) ≈ Matrix(mat(put(nq, locs => gate)))^n | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "inside control block" begin | ||
| for (gate, nq_gate) in [(X, 1), (Rx(0.3), 1), (circ2, 2)] | ||
| ctrl_loc = nq_gate + 1 | ||
| locs = Tuple(1:nq_gate) | ||
| for n in [1, 2, 3] | ||
| pb = gate ^ n | ||
| nq_circ = nq_gate + 1 | ||
| circuit = control(nq_circ, ctrl_loc, locs => pb) | ||
| reg = rand_state(nq_circ) | ||
| reg_ref = copy(reg) | ||
| for _ in 1:n | ||
| apply!(reg_ref, control(nq_circ, ctrl_loc, locs => gate)) | ||
| end | ||
| apply!(reg, circuit) | ||
| @test statevec(reg) ≈ statevec(reg_ref) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unlike
ChainBlockwe know what the content ofPowBlockis. So can we maybe be more specific thanAnyhere because we exactly know what will end up inres?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We just discussed this and concluded that e.g.
RepeatedBlockdoes the sameAny, so this should be fine.