Repository navigation
Add a Power block to yaoblocks - #582
harry-phasecraft wants to merge 26 commits into
Conversation
jlbosse
left a comment
There was a problem hiding this comment.
Thanks a lot for this!
I left some comments to make sure that edge cases work correctly for reverse mode AD and also to make the power constructor match the chain constructor as much as possible.
|
|
||
| function apply_back!(st, block::PowBlock{D, BT, PT}, collector) where {D, BT<:AbstractBlock, PT<:Integer} | ||
| blk = block.pow >= 0 ? content(block) : adjoint(content(block)) | ||
| res = Any[] |
There was a problem hiding this comment.
Unlike ChainBlock we know what the content of PowBlock is. So can we maybe be more specific than Any here because we exactly know what will end up in res?
There was a problem hiding this comment.
We just discussed this and concluded that e.g. RepeatedBlock does the same Any, so this should be fine.
|
@GiggleLiu or @Roger-luo:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #582 +/- ##
==========================================
- Coverage 89.22% 89.18% -0.04%
==========================================
Files 89 90 +1
Lines 5855 5957 +102
==========================================
+ Hits 5224 5313 +89
- Misses 631 644 +13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
I would be leaning to keep
Yes
I believe this was something @GiggleLiu added for coefficient propagation? As long as the main gate tests pass I'm ok with having it, but in case you need it, |
|
One more thing that seems to be missing is decomposition of this into native gates. If I do rules = [
Yao.Optimise.merge_pauli,
Yao.Optimise.merge_scale,
Yao.Optimise.eliminate_nested,
Yao.Optimise.raise_chain,
Yao.Optimise.combine_similar,
Yao.Optimise.to_basictypes
]
simplified_circuit = Yao.Optimise.simplify(circuit, rules=rules)with some |
jlbosse
left a comment
There was a problem hiding this comment.
Main comment is regarding eigenbasis which probably needs a small fix, otherwise this looks good now!
| 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 |
There was a problem hiding this comment.
The eigenvectors of U^n are those of U and the eigenvalues of U^n are the eigenvalues of U raised to the n-th power. So I think this should simply be
function eigenbasis(op::Power)
E, V = eigenbasis(content(op))
return E ^ op.pow, V
endwhere you need to check the E ^ op.pow part again. Currently it might return a Power block again, but we probably want a matblock with the eigenvalues (to the pow-th power) on the diagonal.
| function nparameters(pb::Power) | ||
| return iszero(pb.pow) ? 0 : nparameters(pb.content) | ||
| end |
There was a problem hiding this comment.
Here I am not sure of the iszero check is correct, or whether this should always just return nparameters(content(pb)). We'd have to check the parameter dispatching and reverse mode AD code then.
There was a problem hiding this comment.
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 dispatch etc will work if this returns 0 ?
There was a problem hiding this comment.
We definitely want that dispatch!(circuit, rand(nparameters(circuit)) always works. And it seems to me this might be easier to achieve (that is: with fewer lines of code) if we always return nparameters(content(powblock)) here. This might also simplify the reverse mode AD logic, where we might be able to to avoid branching on pow == 0 and instead just end up with some empty for loops.
And conceptually I guess the thing is that block ^ 0 means "repeat block 0 times" and not "do nothing".
There was a problem hiding this comment.
I made the suggested changes now. Will need QuantumBFS/LuxurySparse.jl#82 though before it fully works.
Co-authored-by: Jan Lukas Bosse <janlukas.bosse@gmail.com>
|
@GiggleLiu or @Roger-luo I believe this is now ready for review from one of you. From my point of view it is all good to merge now. |
|
Thank @jlbosse and @harry-phasecraft , thank you for your contribution. The code quality looks good. I have a routine question: do you have a concrete application that requires this gate type? Whenever we add a new composite block type, it complicates the design. So we expect the gain to be concrete enough. For example here, the autodiff behavior is different from traditional blocks, more chances to have bugs. |
|
I ran a focused review against
Release metadata also looks inconsistent: this changes YaoBlocks and YaoPlots while their versions remain at Stable CI passes. The YaoBlocks nightly failure is in unrelated |
Regarding your review comments: Thanks for the thorough review, I hope they should all be addressed now. Sorry for the delay on this... |
Add a PowBlock. This is a block with a contents and a power that acts as$U^{pow}$ on the circuit.
This helps performance with large repetative circuits.