Skip to content

Commit a987775

Browse files
committed
up
1 parent 98aa6df commit a987775

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

example/floquetify.jl

+75-2
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,90 @@ function has_even_dg1_zx_spiders(zxwd::ZXWDiagram{T,P}) where {T,P}
118118
end
119119

120120

121-
function extract_k_qubit_circuit(zxwd::ZXWDiagram{T,P}) where {T,P}
121+
function dg1_zx_spiders_can_be_start(zxwd::ZXWDiagram{T,P}, model, k_colors::Int) where {T,P}
122+
@variable(model, is_start[1:ZXW.nv(zxwd)], Bin)
123+
# if the spider is a green/red spider with degree more than 1, then it can't be a start
124+
for sp in ZXW.spiders(zxwd)
125+
@match ZXW.spider_type(zxwd,sp) begin
126+
ZXW.Input(_) || ZXW.Output(_) => continue
127+
ZXW.Z(_) || ZXW.X(_) => if !isone(ZXW.degree(zxwd, sp))
128+
@constraint(model, is_start[sp] == 0)
129+
end
130+
end
131+
end
132+
133+
# must have k_colors number of worldlines
134+
@constraint(model, sum(is_start) == k_colors)
135+
return is_start
136+
end
137+
138+
function edge_direction_assignment(zxwd::ZXWDiagram{T,P}, model) where {T,P}
139+
@variable(model, small_idx2large_idx[1:ZXW.ne(zxwd)], Bin)
140+
141+
@variable(model, large_idx2small_idx[1:ZXW.ne(zxwd)], Bin)
142+
143+
edg2idx = Dict{MultipleEdge{Int,Int},Int}()
144+
for edg in ZXW.edges(zxwd)
145+
@constraint(model, small_idx2large_idx[edg] + large_idx2small_idx[edg] <= 1)
146+
end
147+
148+
return small_idx2large_idx, large_idx2small_idx
149+
end
150+
151+
function time_step_assignment(zxwd::ZXWDiagram{T,P}, model, t_steps::Int) where {T,P}
152+
@variable(model, 0 <= time_steps[1:ZXW.nv(zxwd)] <= t_steps, Int)
153+
# no input / output spiders could be assigned a time step
154+
for sp in ZXW.spiders(zxwd)
155+
@match ZXW.spider_type(zxwd,sp) begin
156+
ZXW.Input(_) || ZXW.Output(_) => @constraint(model, time_steps[sp] == 0)
157+
ZXW.Z(_) || ZXW.X(_) => @constraint(model, time_steps[sp] >= 1)
158+
end
159+
end
160+
return time_steps
161+
end
162+
163+
function extract_k_qubit_circuit(zxwd::ZXWDiagram{T,P}, k_colors::Int, t_steps::Int) where {T,P}
122164

123165
has_even_dg1_zx_spiders(zxwd) || error("We don't have even number of degree 1 spiders")
124166
has_only_dg1_3_spiders(zxwd) || error("We don't have only degree 1 or 3 spiders")
125167

126-
models = Model(SCIp.Optimizer)
168+
model = Model(SCIP.Optimizer)
169+
170+
is_start = dg1_zx_spiders_can_be_start(zxwd, model, k_colors)
171+
172+
small_idx2large_idx, large_idx2small_idx = edge_direction_assignment(zxwd, model)
173+
174+
time_steps = time_step_assignment(zxwd, model, t_steps)
175+
176+
127177

178+
179+
# @variable(model, 0 <= time_steps[1:ZXW.nv(zxwd)] <= t_steps, Int)
180+
181+
# # let 0 denote black color
182+
# # let i denote the ith color which denotes the timeline
183+
# @variable(model, 0 <= edge_color[1:ZXW.ne(zxwd)] <= k_colors, Int)
184+
185+
# @variable(model, edge_direction[1:ZXW.ne(zxwd)], Bin)
186+
187+
# @constraint(model,const_name[i in 1:ZXW.ne(zxwd)], edge_color[i] == 0)
188+
189+
@objective(model,Min,1)
190+
optimize!(model)
191+
@assert is_solved_and_feasible(model)
192+
return value.(is_start), value.(small_idx2large_idx), value.(large_idx2small_idx), value.(time_steps)
193+
# return value.(time_steps), value.(edge_color), value.(edge_direction)
128194
# Req2: Causallity must not be violated in the colored version, assign variables to the vertices
129195
# Req4: topology of the finished diagram will be nice, i.e planar
130196
# Isn't this just extracting circuit from ZX-Diagram? It is proven to be #P-Complete. Need to search
197+
end
198+
199+
extract_k_qubit_circuit(four_layer_after_rewrite_zxwd, 2, 2)
200+
131201

202+
for edg in ZXW.edges(four_layer_after_rewrite_zxwd)
203+
@show typeof(edg)
204+
@show edg.src, edg.dst
132205
end
133206

134207
# if need to visualize, use javascript and visualize the modified ZX-diagram

src/ZXW/utils.jl

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function Base.show(io::IO, zxwd::ZXWDiagram{T,P}) where {T<:Integer,P}
132132
end
133133
end
134134

135+
Graphs.edges(zxwd::ZXWDiagram) = edges(zxwd.mg)
135136

136137
"""
137138
nv(zxwd)

0 commit comments

Comments
 (0)