QuantumBFS / Yao.jl

Extensible, Efficient Quantum Algorithm Design for Humans.
https://yaoquantum.org
Other
918 stars 119 forks source link

Kron and chain #430

Closed BoltzmannEntropy closed 1 year ago

BoltzmannEntropy commented 1 year ago

I am having trouble understating the way the 'chain' command works.

image

Mathematically, this should have represented: (I ⊗ Y ⊗ I ⊗ I) (Y ⊗ I ⊗ I ⊗ H) (X ⊗ H ⊗ H ⊗ H) but what i get is that the third row, is mingled into the second row.

circuit = chain(
    4,    
    chain(put(1=>X),repeat(H, 2:4)), #1
    chain(put(1=>Y),put(2=>igate(1)),put(3=>igate(1)),put(4=>H)), #2   
    chain(put(1=>igate(1)),put(2=>Y),put(3=>igate(1)),put(4=>igate(1))), #3        
)
GiggleLiu commented 1 year ago

Thanks for the issue, it is a matter of visualization, (I ⊗ Y ⊗ I ⊗ I) (Y ⊗ I ⊗ I ⊗ H) (X ⊗ H ⊗ H ⊗ H) is equivalent to (Y ⊗ Y ⊗ I ⊗ H) (X ⊗ H ⊗ H ⊗ H). In YaoPlots, the default rule for visualization is:

  1. if you apply an igate on a line, it does not take any slot, and
  2. a gate moves to the first unoccupied slot in a row.

There are two alternative ways to define the circuit that you wanted

Method 1

julia> kron(I2, Y, I2, I2) * kron(Y, I2, I2, H) * kron(X, H, H, H)

Method 2

circuit = chain(
    4,    
    kron(X, H, H, H),
    kron(1=>Y, 4=>H), 
    put(2=>Y),
)
BoltzmannEntropy commented 1 year ago

Thanks for the prompt reply/ I already tried with a Kron command, even your examples above (both method1 and method 2) are not plotted correctly. the Y gate should be on a new slot.

image
GiggleLiu commented 1 year ago

All of them are correct. It is a matter of display. The number of slots in the plot is not equal to the number of blocks in the chain.

BoltzmannEntropy commented 1 year ago

Yes, I understand that, but if I am trying to reproduce Figures from nielsen & chung I am never going to get the same plot while if i use qiskit/cirq I get the exact same plot.

GiggleLiu commented 1 year ago

We can add a new block Barrier to the circuit to help visualization. How does it sounds like?

BoltzmannEntropy commented 1 year ago

Many thanks for your suggestion, I wonder how no one has asked this before, for larger circuits involving many Krons and gates, it would be almost impossible to visually verify a gate without the plots being correctly displayed.

GiggleLiu commented 1 year ago

Hmm, what you said makes sense. Maybe we should add a global variable to toggle the display mode.

BoltzmannEntropy commented 1 year ago

An example in cirq:

image
GiggleLiu commented 1 year ago

Thanks for the example. I added a new global variable in the PR: https://github.com/QuantumBFS/YaoPlots.jl/pull/60 to toggle the barrier mode and compact mode.

The reason why we use compact mode by default is, it do make some circuits more suitible for publication purpose. Please find an example in the comment of that PR.

BoltzmannEntropy commented 1 year ago
image