GTorlai / PastaQ.jl

Package for Simulation, Tomography and Analysis of Quantum Computers
Apache License 2.0
142 stars 23 forks source link

Simplify `getsamples`, qudits, Trotter circuits #241

Closed GTorlai closed 2 years ago

GTorlai commented 2 years ago

This PR implements the following changes:

1) Simplify the interface and internal structure of getsamples. In the new interface, there are only three main modes:

# 1. Sample a state in the computational basis
n = 3
ψ = runcircuit(ghz(n))
getsamples(ψ, 2)
# 3×5 Matrix{Int64}:
#  0  0  0  
#  1  1  1  

# 2. Sample a state in a set of bases
nbases = 2
bases = randombases(n, nbases)

# one measurement per basis
getsamples(ψ, bases)
# 2×3 Matrix{Pair{String, Int64}}:
#  "Y"=>0  "Z"=>0  "Y"=>0  
#  "Z"=>0  "X"=>1  "Y"=>1 

# nshots measurements per basis
nshots = 2
getsamples(ψ, bases, nshots)
# 4×3 Matrix{Pair{String, Int64}}:
#  "Y"=>0  "X"=>1  "Y"=>0
#  "Y"=>1  "X"=>0  "Y"=>0
#  "Y"=>1  "Z"=>1  "Z"=>1
#  "Y"=>0  "Z"=>0  "Z"=>0

# 3. Sample a unitary (or Choi matrix) for a set of input states and measurement bases
 U = runcircuit(ghz(n); process = true)
npreps = 2
nbases = 3
preps = randompreparations(n, npreps)
bases = randombases(n, nbases)
getsamples(U, preps, bases)
# 6×3 Matrix{Pair{String, Pair{String, Int64}}}:
#  "Z-"=>("Z"=>0)  "X-"=>("Z"=>0)  "X+"=>("Z"=>1)
#  "Z-"=>("Z"=>0)  "X-"=>("Y"=>0)  "X+"=>("Z"=>1)
#  "Z-"=>("Z"=>1)  "X-"=>("Z"=>1)  "X+"=>("X"=>0)
#  "Y-"=>("Z"=>1)  "X+"=>("Z"=>0)  "Y+"=>("Z"=>1)
#  "Y-"=>("Z"=>0)  "X+"=>("Y"=>1)  "Y+"=>("Z"=>1)
#  "Y-"=>("Z"=>1)  "X+"=>("Z"=>1)  "Y+"=>("X"=>1)

# if `nshots` is also passed, get `nshots` per prep/basis configuration

2) Introduce support for qudit gates. Everything applies as per usual:

# annihilation operator for 3-level system
gate("a", 3)
# 3×3 Matrix{Float64}:
#  0.0  1.0  0.0
#  0.0  0.0  1.41421
#  0.0  0.0  0.0

# exchange gate between a qudit and a qubit
gate("a†a",(3,2))
# 6×6 Matrix{Float64}:
#  0.0  0.0  0.0  0.0      0.0  0.0
#  0.0  0.0  0.0  0.0      0.0  0.0
#  0.0  1.0  0.0  0.0      0.0  0.0
#  0.0  0.0  0.0  0.0      0.0  0.0
#  0.0  0.0  0.0  1.41421  0.0  0.0
#  0.0  0.0  0.0  0.0      0.0  0.0

3) Introduce automated (first and second order) Trotter-Suzuki decomposition of an input Hamiltonian. The gates are re-arranged as a staircase:

H = OpSum() + (-1.0, "ZZ",1,2) +  (-1.0, "ZZ",5,1) + (-1.0, "ZZ",4,2) + ( -1.0, "ZZ",3,5) + (0.5,"X",2) + (1.5,"Y",3)

T = 1.0
circuit = trottercircuit(H, T; δt = 0.1, layered = false)
# 100-element Vector{Tuple}:
#  ("ZZ", (1, 2), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (5, 1), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (4, 2), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (3, 5), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("X", 2, (f = PastaQ.var"#856#863"{Float64, ComplexF64}(0.1, 0.5 + 0.0im),))
#  ("Y", 3, (f = PastaQ.var"#856#863"{Float64, ComplexF64}(0.1, 1.5 + 0.0im),))
#  ("ZZ", (3, 5), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (4, 2), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (5, 1), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (1, 2), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (1, 2), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))
#  ("ZZ", (5, 1), (f = PastaQ.var"#857#864"{Float64, ComplexF64}(0.1, -1.0 + 0.0im),))

Other minor changes:

codecov-commenter commented 2 years ago

Codecov Report

Merging #241 (e391342) into master (1655bdb) will decrease coverage by 0.84%. The diff coverage is 82.30%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #241      +/-   ##
==========================================
- Coverage   85.75%   84.90%   -0.85%     
==========================================
  Files          22       23       +1     
  Lines        2408     2458      +50     
==========================================
+ Hits         2065     2087      +22     
- Misses        343      371      +28     
Impacted Files Coverage Δ
src/PastaQ.jl 100.00% <ø> (ø)
src/circuits/runcircuit.jl 83.65% <ø> (+0.96%) :arrow_up:
src/circuits/trottersuzuki.jl 70.00% <70.00%> (ø)
src/circuits/gates.jl 86.81% <76.92%> (-3.12%) :arrow_down:
src/productstates.jl 91.48% <86.66%> (ø)
src/circuits/noise.jl 92.94% <88.23%> (+1.80%) :arrow_up:
src/utils.jl 75.63% <88.88%> (+0.85%) :arrow_up:
src/circuits/getsamples.jl 87.05% <95.00%> (+23.00%) :arrow_up:
src/array.jl 97.29% <100.00%> (+0.23%) :arrow_up:
src/circuits/circuits.jl 100.00% <100.00%> (+1.12%) :arrow_up:
... and 6 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1655bdb...e391342. Read the comment docs.