Skip to content

Commit

Permalink
improvements to printing (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Jun 10, 2023
1 parent 2811656 commit 40a2cf1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
38 changes: 9 additions & 29 deletions docs/src/stab-algebra-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ an undetermined result). For instance here we project on an operator that does
not commute with all stabilizer generators.

```jldoctest proj
julia> project!(copy(s), P"ZII")
(+ Z__
julia> project!(copy(s), P"ZII")[1]
+ Z__
+ ZZ_
- _ZZ, 1, nothing)
- _ZZ
```

Importantly, when there is an undetermined result, we return `nothing` **and
Expand All @@ -325,9 +325,7 @@ to represent the phase, 0x2).

```jldoctest proj
julia> project!(copy(s), P"-ZZI")
(- XXX
- Z_Z
- _ZZ, 0, 0x02)
(Stabilizer 3×3, 0, 0x02)
```

When the projection is consistent with the stabilizer (i.e. the measurement
Expand All @@ -340,19 +338,15 @@ canonicalization and calculation of the result.

```jldoctest proj
julia> project!(copy(s), P"-ZZI", keep_result=false)
(- XXX
+ ZZ_
- _ZZ, 0, nothing)
(Stabilizer 3×3, 0, nothing)
```

Lastly, in either case, you can skip the calculation of the phases as well, if
they are unimportant.

```jldoctest proj
julia> project!(copy(s), P"ZZI", phases=false)
(- XXX
+ Z_Z
- _ZZ, 0, 0x00)
(Stabilizer 3×3, 0, 0x00)
```

## Sparse single-qubit measurements
Expand Down Expand Up @@ -596,7 +590,7 @@ They are used slightly differently, as one needs to specify the qubits on which

```jldoctest
julia> sHadamard(2)
Symbolic single-qubit gate on qubit 2
sHadamard on qubit 2
X₁ ⟼ + Z
Z₁ ⟼ + X
Expand Down Expand Up @@ -655,28 +649,14 @@ the expensive ``\mathcal{O}(n^3)`` canonicalization operation).

```jldoctest destab
julia> project!(d,P"ZZI")
(𝒟ℯ𝓈𝓉𝒶𝒷
+ Z__
+ _X_
+ __X
𝒮𝓉𝒶𝒷━
- XXX
- ZZ_
- Z_Z, 0, 0x02)
(Destablizer 3×3, 0, 0x02)
```

Non-commuting projections are just as fast as when using only stabilizers.

```jldoctest destab
julia> project!(d,P"ZZZ")
(𝒟ℯ𝓈𝓉𝒶𝒷
- XXX
+ X_X
+ XX_
𝒮𝓉𝒶𝒷━
+ ZZZ
- ZZ_
- Z_Z, 1, nothing)
(Destablizer 3×3, 1, nothing)
```

Clifford operations can be applied the same way they are applied to stabilizers.
Expand Down
9 changes: 6 additions & 3 deletions src/canonicalization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ julia> s = S"XXXX
ZIIZ";
julia> canonicalize!(s, ranks=true)
(+ XXXX
julia> _, ix, iz = canonicalize!(s, ranks=true); ix, iz
(1, 3)
julia> s
+ XXXX
+ Z__Z
+ _Z_Z
+ ____, 1, 3)
+ ____
```
Based on [garcia2012efficient](@cite).
Expand Down
24 changes: 12 additions & 12 deletions src/project_trace_reset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ julia> s = S"XZI
IZI";
julia> project!(s, P"IIX")
(+ X__
+ _Z_, 3, nothing)
julia> project!(s, P"IIX")[1]
+ X__
+ _Z_
```
If we had used [`MixedStabilizer`](@ref) we would have added the projector
Expand All @@ -193,10 +193,10 @@ julia> s = one(MixedStabilizer, 2, 3)
+ Z__
+ _Z_
julia> project!(s, P"IIX")
(+ Z__
julia> project!(s, P"IIX")[1]
+ Z__
+ _Z_
+ __X, 3, nothing)
+ __X
```
However, [`MixedDestabilizer`](@ref) would
Expand All @@ -216,15 +216,15 @@ julia> s = one(MixedDestabilizer, 2, 3)
𝒵ₗ━━━
+ __Z
julia> project!(s, P"IIX")
(𝒟ℯ𝓈𝓉𝒶𝒷
julia> project!(s, P"IIX")[1]
𝒟ℯ𝓈𝓉𝒶𝒷
+ X__
+ _X_
+ __Z
𝒮𝓉𝒶𝒷━
+ Z__
+ _Z_
+ __X, 3, nothing)
+ __X
```
See the "Datastructure Choice" section in the documentation for more details.
Expand Down Expand Up @@ -253,10 +253,10 @@ julia> ms = MixedStabilizer(s)
+ X__
+ _Z_
julia> project!(ms, P"IIY")
(+ X__
julia> project!(ms, P"IIY")[1]
+ X__
+ _Z_
+ __Y, 3, nothing)
+ __Y
```
Similarly to [`project!`](@ref) on [`Stabilizer`](@ref), this function has cubic
Expand Down
18 changes: 13 additions & 5 deletions src/symbolic_cliffords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
```jldoctest
julia> op = SingleQubitOperator(2, true, true, true, false, true, true) # Tableau components and phases
Symbolic single-qubit gate on qubit 2
SingleQubitOperator on qubit 2
X₁ ⟼ - Y
Z₁ ⟼ - X
Expand Down Expand Up @@ -196,8 +196,12 @@ end
CliffordOperator(::Type{O}) where {O<:AbstractSingleQubitOperator} = CliffordOperator(apply!(one(Destabilizer,1),O(1)))

function Base.show(io::IO, op::AbstractSingleQubitOperator)
print(io, "Symbolic single-qubit gate on qubit $(op.q)\n")
show(io, CliffordOperator(op,1;compact=true))
if get(io, :compact, false) | haskey(io, :typeinfo)
print(io, "$(string(typeof(op)))($(op.q))")
else
print(io, "$(string(typeof(op))) on qubit $(op.q)\n")
show(io, CliffordOperator(op,1;compact=true))
end
end

"""Random symbolic single-qubit Clifford applied to qubit at index `qubit`.
Expand Down Expand Up @@ -292,8 +296,12 @@ end
CliffordOperator(::Type{O}) where {O<:AbstractTwoQubitOperator} = CliffordOperator(apply!(one(Destabilizer,2),O(1,2)))

function Base.show(io::IO, op::AbstractTwoQubitOperator)
print(io, "Symbolic two-qubit gate on qubit $(op.q1) and $(op.q2)\n")
show(io, CliffordOperator(typeof(op)))
if get(io, :compact, false) | haskey(io, :typeinfo)
print(io, "$(string(typeof(op)))($(op.q1),$(op.q2))")
else
print(io, "$(string(typeof(op))) on qubit1 ($(op.q1),$(op.q2))\n")
show(io, CliffordOperator(op,2;compact=true))
end
end

##############################
Expand Down
16 changes: 8 additions & 8 deletions src/tableau_show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end
_show(io::IO, p::PauliOperator, limit=50) = print(io, ["+ ","+i","- ","-i"][p.phase[]+1]*xz2str_limited(xbit(p),zbit(p), limit))

function Base.show(io::IO, p::PauliOperator)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
_show(io, p, 10)
elseif get(io, :limit, false)
sz = displaysize(io)
Expand Down Expand Up @@ -41,7 +41,7 @@ function _show(io::IO, t::Tableau, limit=50, limit_vertical=20)
end

function Base.show(io::IO, t::Tableau)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
r,q = size(t)
print(io, "Tableaux $r×$q")
elseif get(io, :limit, false)
Expand All @@ -53,7 +53,7 @@ function Base.show(io::IO, t::Tableau)
end

function Base.show(io::IO, s::Stabilizer)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
r,q = size(s)
print(io, "Stabilizer $r×$q")
else
Expand All @@ -62,7 +62,7 @@ function Base.show(io::IO, s::Stabilizer)
end

function Base.show(io::IO, s::MixedStabilizer)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
r,q = size(stabilizerview(s))
print(io, "MixedStabilizer $r×$q")
else
Expand All @@ -71,7 +71,7 @@ function Base.show(io::IO, s::MixedStabilizer)
end

function Base.show(io::IO, d::Destabilizer)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
r,q = size(stabilizerview(d))
print(io, "Destablizer $r×$q")
elseif get(io, :limit, false)
Expand All @@ -91,7 +91,7 @@ end
function Base.show(io::IO, d::MixedDestabilizer)
r = rank(d)
q = nqubits(d)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
print(io, "MixedDestablizer $r×$q")
elseif get(io, :limit, false)
h,w = displaysize(io)
Expand Down Expand Up @@ -159,9 +159,9 @@ function _show(io::IO, c::CliffordOperator, limit=50, limit_vertical=20)
end

function Base.show(io::IO, c::CliffordOperator)
if get(io, :compact, false)
if get(io, :compact, false) | haskey(io, :typeinfo)
q = nqubits(c)
print(io, "Clifford $q qubits")
print(io, "CliffordOperator on $q qubits")
elseif get(io, :limit, false)
sz = displaysize(io)
_show(io, c, sz[2], sz[1])
Expand Down

0 comments on commit 40a2cf1

Please sign in to comment.