# What does Yao.Measure do with argument operator?

**URL:** <https://discourse.julialang.org/t/what-does-yao-measure-do-with-argument-operator/125546>\
**Category:** Quantum\
**Created:** [February 4, 2025, 5:49pm UTC](https://discourse.julialang.org/t/what-does-yao-measure-do-with-argument-operator/125546 "2025-02-04T17:49:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yao\_novice](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yao_novice/32/212085_2.png) [@yao\_novice](https://discourse.julialang.org/u/yao_novice)\
**Post date:** [February 4, 2025, 5:49pm UTC](https://discourse.julialang.org/t/what-does-yao-measure-do-with-argument-operator/125546/1 "2025-02-04T17:49:05Z")

</div>

I experiment with Yao.jl and I am puzzled with the ‘Measure’-blocks ‘operator’ argument. I assumed that the input state would end up with an eigenstate of the operator-matrix - but this seems not to be true.  
Here is my code and arguments in the comments

```julia
using Yao, LinearAlgebra

# only for simplifying the output
function simplify(A)
    if isreal(A) 
        A = round.(real.(A), digits=4)
    end
    if reduce(*,collect(Iterators.flatten(isinteger.(A))))
        A = round.(Int64,A)
    end
    return A
end 
⊗(a,b)=kron(a,b)
θ = π/8
op_block= Ry(2*θ) * Z * Ry(-2*θ)
bell = ArrayReg(1/sqrt(2)*[1.0+0.0im;0;0;1])
inp = copy(bell) # we save bell for later use
#--------- IMPORTANT ----------------------
inp|> Measure(2, locs=1, operator=op_block)
# inp is NOW collapsed state  
println("measure at 1: ",simplify(state(inp)))
#=
OUTPUT:
measure at 1: [0.1464; -0.3536; -0.3536; 0.8536;;]
measure all: [0.8536; 0.3536; 0.3536; 0.1464;;]
OR
measure at 2: [0.8536; 0.3536; 0.3536; 0.1464;;]
measure all: [0.1464; -0.3536; -0.3536; 0.8536;;]
-------------------------------------------------
=#

#=
the inp-state should now be in an eigenstate of the 
operator 'op_full_mat = Matrix(mat(op_block) ⊗ mat(I2))'
'op_full_mat' is Hermitian and unary and has
eigenstates with eigenvalues ±1 :
v1≈[0.92; 0; 0.38; 0] v2≈[0; 0.92; 0; 0.38]
v3≈[-0.38; 0; 0.92; 0] v4≈[0; -0.38; 0; 0.92;]
Bell can be decribed in this basis:

bell = Φ⁺ = 0.65*v1 + 0.27*v2 -0.27*v3 + 0.65*v4
(since Φ⁺ is a unit vector this is one too)
where I would interpret '0.65' as a probability amplitude that
the system collapses to eigenvector v1 -> P(v1)= 0.65^2

By the way: The output reveals only 2 different results -
but there are 4 eigenstates of 'op_full_mat' ???
No difference if locs=1 or locs=2 or AllLocs ?

WHERE is my misconception??

=#

# we now extend the operator Identity at least significant qubit
op_full_mat = Matrix(mat(op_block) ⊗ mat(I2))
op_full = GeneralMatrixBlock(op_full_mat)
inp = copy(bell) # we save bell for later use
inp|> Measure(2, operator=op_full)
# inp is NOW collapsed state  
println("measure all: ",simplify(state(inp)))

# we now extend the operator Identity a least significant qubit
op_full_mat = Matrix(mat(I2) ⊗ mat(op_block) )
op_full = GeneralMatrixBlock(op_full_mat)
inp = copy(bell) # we save bell for later use
inp|> Measure(2, operator=op_full)
# inp is NOW collapsed state  
display(simplify(state(inp)))

```

Has anybody a hint WHERE am I wrong with my thoughts?

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [February 5, 2025, 3:05am UTC](https://discourse.julialang.org/t/what-does-yao-measure-do-with-argument-operator/125546/2 "2025-02-05T03:05:04Z")

</div>

> The output reveals only 2 different results - there are 4 eigenstates of ‘op\_full\_mat’ ???

If the operator has degenerate eigenvalues, the measurement operation collapses the state to a subspace, rather than a single eigenvector.

```julia
julia> eigen(Matrix(mat(op_block) ⊗ mat(I2))).values
4-element Vector{Float64}:
 -0.9999999999999984
 -0.9999999999999984
  1.0
  1.0

```

---

<div class="post-metadata">

**Author:** ![yao\_novice](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yao_novice/32/212085_2.png) [@yao\_novice](https://discourse.julialang.org/u/yao_novice)\
**Post date:** [February 5, 2025, 6:49am UTC](https://discourse.julialang.org/t/what-does-yao-measure-do-with-argument-operator/125546/3 "2025-02-05T06:49:46Z")

</div>

When I constructed the eigenstates  
\begin{pmatrix} \cos(\theta) \\ \sin(\theta) \end{pmatrix} \otimes |0\rangle  
and so on … I did know the eigenvalues remained -1,+1 but I had no idea that this leads to a degeneration: 4 states, 2 values.  
I thought of this for weeks and you solved it in a minute - thumbs up  
and thank you so much (you prevented me from headaches)  
This topic is solved
