Skip to content

perf: VQE ablation tests #1416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions ext/ReactantYaoBlocksExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ function YaoBlocks.mat(
M = broadcast_to_size(zero(T), (2, 2))
c = cos(R.theta / 2)
s = -im * sin(R.theta / 2)
M[1, 1] = c
M[2, 2] = c
M[1, 2] = s
M[2, 1] = s
@allowscalar begin
M[1, 1] = c
M[2, 2] = c
M[1, 2] = s
M[2, 1] = s
end
return M
end

Expand All @@ -23,10 +25,12 @@ function YaoBlocks.mat(
M = broadcast_to_size(zero(T), (2, 2))
c = cos(R.theta / 2)
s = sin(R.theta / 2)
M[1, 1] = c
M[2, 2] = c
M[1, 2] = -s
M[2, 1] = s
@allowscalar begin
M[1, 1] = c
M[2, 2] = c
M[1, 2] = -s
M[2, 1] = s
end
return M
end

Expand All @@ -35,8 +39,10 @@ function YaoBlocks.mat(
) where {D,T,S}
M = broadcast_to_size(zero(T), (2, 2))
x = exp(im * R.theta / 2)
M[1, 1] = conj(x)
M[2, 2] = x
@allowscalar begin
M[1, 1] = conj(x)
M[2, 2] = x
end
return M
end

Expand Down
132 changes: 132 additions & 0 deletions perf/vqe/Circuit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Tangles
using Networks
using LinearAlgebra
using YaoBlocks
using DelegatorTraits
import DelegatorTraits: DelegatorTrait

@kwdef struct Circuit <: Tangles.AbstractTensorNetwork
tn::Tangles.GenericTensorNetwork
last_t::Dict{Site,Int} = Dict{Site,Int}()
end

DelegatorTrait(::Networks.Network, ::Circuit) = DelegateToField{:tn}()
DelegatorTrait(::Networks.Taggable, ::Circuit) = DelegateToField{:tn}()
DelegatorTrait(::Tangles.TensorNetwork, ::Circuit) = DelegateToField{:tn}()
DelegatorTrait(::Tangles.Pluggable, ::Circuit) = DelegateToField{:tn}()
DelegatorTrait(::Tangles.Lattice, ::Circuit) = DelegateToField{:tn}()

Base.copy(circ::Circuit) = Circuit(copy(circ.tn), Dict{Site,Int}())

function flatten_circuit(x)
if any(i -> i isa ChainBlock, subblocks(x))
flatten_circuit(YaoBlocks.Optimise.eliminate_nested(x))
else
x
end
end

struct LaneAt{S}
site::S
t::Int
end

function Base.show(io::IO, lane::LaneAt)
print(io, lane.site)
return print(io, "@$(lane.t)")
end

moment(circuit::Circuit, site::Site) = circuit.last_t[site]

"""
Convert a Yao circuit to a Circuit.
"""
function Base.convert(::Type{Circuit}, yaocirc::AbstractBlock)
tn = GenericTensorNetwork()
circuit = Circuit(tn, Dict{Site,Int}())

for gate in flatten_circuit(yaocirc)
# if gate isa Swap
# (a, b) = occupied_locs(gate)
# wire[a], wire[b] = wire[b], wire[a]
# continue
# end

gatesites = CartesianSite.(occupied_locs(gate))
gateinds = Index.([Plug.(gatesites)..., Plug.(gatesites; isdual=true)...])

# NOTE `YaoBlocks.mat` on m-site qubits still returns the operator on the full Hilbert space
m = length(occupied_locs(gate))
operator = if gate isa YaoBlocks.ControlBlock
control((1:(m - 1))..., m => content(gate))(m)
else
content(gate)
end

array = reshape(collect(mat(operator)), fill(nlevel(operator), length(gateinds))...)

addtensor!(circuit, Tensor(array, gateinds))
end

return circuit
end

function Tangles.addtensor!(circuit::Circuit, tensor::Tensor)
target_plugs = plugs(tensor)
target_plugs_in = filter(isdual, target_plugs)
target_plugs_out = filter(!isdual, target_plugs)
target_sites = unique!(site.(target_plugs))

# if lane is not present, add an identity gate
for plug in adjoint.(target_plugs_in)
if !hasplug(circuit, plug)
input, out = Index(LaneAt(site(plug), 1)), Index(LaneAt(site(plug), 2))
addtensor!(circuit.tn, Tensor([1 0; 0 1], [input, out]))
setplug!(circuit, input, plug')
setplug!(circuit, out, plug)
circuit.last_t[site(plug)] = 2
end
end

# align gate tensor with the circuit
tensor = replace(
tensor,
[
Index(plug) => Index(LaneAt(site(plug), moment(circuit, site(plug)))) for
plug in target_plugs_in
]...,
[
Index(plug) => Index(LaneAt(site(plug), moment(circuit, site(plug)) + 1)) for
plug in target_plugs_out
]...,
)

addtensor!(circuit.tn, tensor)

# update plug tags mapping
for plug in target_plugs_out
unsetplug!(circuit, plug)
setplug!(circuit, Index(LaneAt(site(plug), moment(circuit, site(plug)) + 1)), plug)
end

# update the last_t for each site
for site in target_sites
circuit.last_t[site] = circuit.last_t[site] + 1
end

return circuit
end

"""
Create an observable type Circuit.
"""
function create_observable(N)
observable = chain(N, [put(i => Z) for i in 1:N]...) # Observable to measure: Z gate on each qubit

# convert observable to circuit
observable = convert(Circuit, observable)
for tensor in tensors(observable)
replace!(observable, tensor => Tensor(real(collect(parent(tensor))), inds(tensor)))
end
return observable
end
30 changes: 30 additions & 0 deletions perf/vqe/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CliqueTrees = "60701a23-6482-424a-84db-faee86b9b1f8"
DelegatorTraits = "d83dbd90-3e00-4b33-84f1-398adcf3b35e"
EinExprs = "b1794770-133b-4de1-afb4-526377e9f4c5"
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Metis = "2679e427-3c69-5b7f-982b-ece356f1e94b"
Muscle = "21fe5c4b-a943-414d-bf3e-516f24900631"
Networks = "634dcf3f-6aa7-47ba-9e85-1fb329c9ebfe"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Reactant = "3c362404-f566-11ee-1572-e11a4b42c853"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Tangles = "d2150e0e-da4c-4196-bcf0-8e0aeaf6db16"
Tenet = "85d41934-b9cd-44e1-8730-56d86f15f3ec"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
YaoBlocks = "418bc28f-b43b-5e0b-a6e7-61bbc1a2c1df"

# Reactant fixed to 0.2.138 due to a bug in rev diff rule of stablehlo.dynamic_update_slice
# [sources]
# Reactant = {path = "../../"}

[compat]
EinExprs = "0.6.10"
Enzyme = "0.13.51"
Muscle = "0.3.14"
Reactant = "=0.2.138"
Tenet = "0.10.0"
YaoBlocks = "0.13.15"
18 changes: 18 additions & 0 deletions perf/vqe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Variational Quantum Eigensolver (VQE)

A simple VQE training through exact tensor network contraction and backpropagation.
Because it uses exact tensor network contraction, it cannot scale to large number of layers but it should be able to scale to mid-range number of qubits (around 50) and shallow circuits.

It uses a "Efficient SU(2)" circuit ansatz for the VQE.

## Setup

Tenet.jl and Tangles.jl are not registered in the General registry of packages.
You need to add the Quantic registry of packages for Julia to find them:

```julia
]registry add https://github.com/bsc-quantic/Registry.git
```

> [!WARNING]
> Reactant.jl version is fixed to 0.2.138 due to a bug with the rev diff rule of `stablehlo.dynamic_update_slice`
128 changes: 128 additions & 0 deletions perf/vqe/hamiltonian-terms-n20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
-20.02329899902102 [X0 Y1 X8 Z9 Z10 Z11 Z12 Z13 Z14 Y15]
0.10767345306547756 []
8.80734867719193e-5 [X0 Y1 X2 Y3]
-0.00031909186414867024 [X0 Y1 Y12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
0.0004996872413672634 [X0 Y1 Y5 Z6 Z7 Z8 Z9 Z10 Z11 X12]
0.016657629396396256 [X0 X1 X8 Z9 Z10 Z11 Z12 Z13 Z14 X15]
-2.509112878277348e-5 [X0 X1 Y5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Y18]
-7.526057730086852e-5 [X0 Y1 X3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Y12]
0.00016804175381690398 [X0 Z1 X2 X5 Z6 Z7 Z8 Z9 Z10 X11]
-3.2989480400377e-5 [X0 X1 Y8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Y17]
-4.2292656360584896e-5 [X0 Z1 X2 Y7 Z8 Y9]
0.00019801033544939237 [X0 Z1 X2 X7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 X17]
0.00012335352619074634 [X0 Y1 Y15 X16]
-0.00031653544691587204 [X0 X1 Y14 Y15]
0.0004572073593218358 [X0 Y1 Y3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 X12]
0.02768448501770293 [X0 X1 Y9 Z10 Z11 Z12 Z13 Y14]
-0.0009252706068446522 [X0 Y1 X6 Y7]
0.0049072637209679314 [X0 Y1 X13 Z14 Z15 Z16 Z17 Y18]
0.12811100236340767 [X0 X1 X6 X7]
-0.0010270313650730257 [X0 X1 X14 X15]
0.0027427495558326985 [X0 Y1 Y16 X17]
-0.0013614634317282404 [X0 X1 Y6 Y7]
0.0010176818550736016 [X0 Z1 X2 X7 Z8 X9]
-0.0003828743465888113 [X0 X1 Y12 Y13]
0.0032512111489067467 [X0 X1 Y18 Y19]
0.08411434541978045 [X0 X1 Y14 Z15 Z16 Y17]
0.05855039299635987 [X0 Z1 X2 Y4 Z5 Z6 Z7 Z8 Z9 Y10]
0.09095271633361096 [X0 X1 X4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
-0.001343764804653726 [X0 Z1 X2 Y7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Y17]
-0.001784854131285513 [X0 Y1 Y4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
0.06648285866273813 [X0 X1 X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 X13]
-0.002471090646759579 [X0 X1 Y3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Y18]
-0.002646396087864506 [X0 X1 X4 X5]
-0.0006516063459811195 [X0 Y1 Y8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 X17]
-0.0007257668661868602 [X0 Z1 X2 Y6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Y16]
-0.00022707152770222623 [X0 Y1 Y14 X15]
-0.0012325380495824597 [X0 Y1 Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 X13]
0.06331251922594161 [X0 Z1 X2 Y6 Z7 Y8]
0.028867052831104578 [X0 X1 Y2 Z3 Z4 Y5]
0.06542879037731869 [X0 X1 X8 X9]
-0.00014533380020575353 [X0 X1 Y13 Z14 Z15 Z16 Z17 Y18]
0.07232772561954542 [X0 X1 X14 Z15 Z16 X17]
-1.4357805341064722e-5 [X0 X1 X10 X11]
-0.00013807133519685261 [X0 Y1 X9 Z10 Z11 Z12 Z13 Z14 Z15 Y16]
-0.0005050059022825259 [X0 X1 X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
0.06137357140003864 [X0 X1 Y4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Y13]
0.0034881204936011937 [X0 Y1 Y4 X5]
0.029955230601382776 [X0 Y1 X14 Z15 Z16 Y17]
0.014847441760803421 [X0 Y1 X3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Y18]
0.002125311725514915 [X0 Z1 X2 X3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 X11]
0.001564691392240837 [X0 Y1 X15 Y16]
0.019178836293542154 [X0 Y1 Y4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 X13]
0.02542595818030917 [X0 Z1 X2 X6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 X16]
0.010018665280409095 [X0 X1 Y16 Y17]
0.05703094103843582 [X0 Z1 X2 X6 Z7 X8]
0.022700074315972207 [X0 X1 Y10 Y11]
2.7688129380049205e-5 [X0 Y1 Y6 X7]
-9.886346204475988e-5 [X0 X1 Y4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
0.00039034474838729196 [X0 X1 Y3 Y4]
-0.0277167485222872 [X0 X1 Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
6.242787825755139e-6 [X0 X1 X18 X19]
0.0002855444044645041 [X0 Y1 Y2 Z3 Z4 X5]
-0.0001040459934573164 [X0 X1 Y12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
-9.000790603273486e-5 [X0 Y1 X4 Y5]
0.00022974417125316116 [X0 X1 X13 Z14 Z15 Z16 Z17 X18]
-0.00014812821038365177 [X0 X1 Y8 Z9 Z10 Z11 Z12 Z13 Z14 Y15]
-2.0221779377688277e-5 [X0 Y1 X12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
-5.1452044053781085e-5 [X0 Y1 X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Y13]
7.86548453763452e-5 [X0 X1 X2 Z3 Z4 X5]
0.13540765122361054 [X0 Z1 X2 X10 Z11 X12]
-0.001179016832287718 [X0 Y1 Y8 X9]
0.007367320617259539 [X0 X1 X12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
-0.07350243538294754 [X0 X1 X9 Z10 Z11 Z12 Z13 X14]
0.0012763873888146974 [X0 X1 Y4 Y5]
-0.0024253471132371623 [X0 X1 Y9 Z10 Z11 Z12 Z13 Z14 Z15 Y16]
0.0004825269432643367 [X0 Y1 Y9 Z10 Z11 Z12 Z13 Z14 Z15 X16]
-0.003339862159534003 [X0 Y1 X2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
-6.280756169407885e-5 [X0 X1 X3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 X12]
0.008304253322401048 [X0 Y1 X4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Y13]
0.04417469435125109 [X0 Y1 Y9 Z10 Z11 Z12 Z13 X14]
-0.04542961161142746 [X0 Y1 Y8 Z9 Z10 Z11 Z12 Z13 Z14 X15]
0.00889488443332749 [X0 X1 X12 X13]
-0.0021101166617534307 [X0 Y1 Y12 X13]
-0.0016956381013258253 [X0 Z1 X2 X6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 X14]
-0.0654742474046646 [X0 Y1 Y13 Z14 Z15 Z16 Z17 X18]
0.0031562305123940793 [X0 Z1 X2 Y7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Y15]
0.00187745121935422 [X0 Y1 X4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Y19]
0.0002771421459178943 [X0 X1 Y2 Y3]
0.0007977013979872697 [X0 Y1 Y14 Z15 Z16 X17]
-0.0009970033920598462 [X0 Y1 X8 Y9]
-0.001556263334000379 [X0 Z1 X2 X4 Z5 Z6 Z7 Z8 Z9 X10]
0.08169718548652216 [X0 Y1 Y18 X19]
-0.0602534590081775 [X0 X1 Y5 Z6 Z7 Z8 Z9 Z10 Z11 Y12]
0.05894421697511449 [X0 Y1 X16 Y17]
-0.00010671533930654679 [X0 Y1 X18 Y19]
-0.09636936857412182 [X0 Y1 Y10 X11]
0.0003218263773590805 [X0 X1 X5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 X18]
0.0001883154879879538 [X0 X1 X8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 X17]
-0.0002071683425504752 [X0 X1 X16 X17]
0.008552681875402257 [X0 Y1 X2 Z3 Z4 Y5]
0.010430073279025544 [X0 X1 X9 Z10 Z11 Z12 Z13 Z14 Z15 X16]
-0.005621787748994994 [X0 X1 X15 X16]
-0.014632424919220887 [X0 Y1 X5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Y18]
-0.003438351538167895 [X0 Z1 X2 X7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 X15]
0.0015260649862089606 [X0 X1 X2 X3]
0.036188529251918955 [X0 Z1 X2 Y3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Y11]
-0.10295717032794717 [X0 X1 Y3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Y12]
0.01258641195127333 [X0 X1 X5 Z6 Z7 Z8 Z9 Z10 Z11 X12]
-0.07944403824155766 [X0 Z1 X2 Y6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Y14]
5.092602816399091e-5 [X0 Y1 X14 Y15]
5.524983047072962e-5 [X0 Y1 Y3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 X18]
9.072130446166333e-5 [X0 X1 Y8 Y9]
-3.958328345307478e-5 [X0 Y1 X10 Y11]
-9.92325667558302e-7 [X0 X1 X3 X4]
-3.716513732689877e-7 [X0 Y1 Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 X19]
9.169893538395603e-6 [X0 X1 X3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 X18]
-5.356347396773038e-6 [X0 X1 X4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 X13]
2.0502317101097673e-6 [X0 Y1 X5 Z6 Z7 Z8 Z9 Z10 Z11 Y12]
-0.0023500087935177803 [X0 X1 Y15 Y16]
3.755602289555965e-5 [X0 Y1 X3 Y4]
-4.860586735184505e-5 [X0 X1 Y2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Y13]
-0.0012573532197818287 [X0 Y1 Y5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 X18]
9.063813115828972e-6 [X0 Y1 X8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Y17]
-2.6142212088227807e-5 [X0 Y1 Y3 X4]
-0.0011022929384773205 [X0 Y1 Y2 X3]
1.6494258663913302e-5 [X0 Y1 X9 Z10 Z11 Z12 Z13 Y14]
-9.597455750374637e-6 [X0 Y1 X12 Y13]
0.00015234277868995082 [X0 Z1 X2 Y5 Z6 Z7 Z8 Z9 Z10 Y11]
Loading
Loading