Skip to content

Commit d6018f8

Browse files
authored
Fix type instabilities in compactified methods (due to type mismatch in the uncompactified method) (#265)
``` function x_diag_circuit_noisy_measurement(csize) circuit = [] for i in 1:csize push!(circuit, PauliError(i, 0.1)) push!(circuit, sHadamard(i)) push!(circuit, sCNOT(i, csize+1)) push!(circuit, sMZ(csize+1,i)) push!(circuit, ClassicalXOR((1,(i%6+6)),i)) end return circuit end @benchmark pftrajectories(state,circuit) setup=(state=PauliFrame(1000, 1001, 1001); circuit=compactify_circuit(x_diag_circuit_noisy_measurement(1000))) evals=1 Before: BenchmarkTools.Trial: 10 samples with 1 evaluation. Range (min … max): 2.885 ms … 2.962 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 2.900 ms ┊ GC (median): 0.00% Time (mean ± σ): 2.912 ms ± 30.387 μs ┊ GC (mean ± σ): 0.00% ± 0.00% █▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ ██▁█▁▁▁█▁▁▁▁▁▁█▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁█▁▁█ ▁ 2.89 ms Histogram: frequency by time 2.96 ms < Memory estimate: 187.50 KiB, allocs estimate: 4000. After: BenchmarkTools.Trial: 749 samples with 1 evaluation. Range (min … max): 2.929 ms … 3.097 ms ┊ GC (min … max): 0.00% … 0.00% Time (median): 2.948 ms ┊ GC (median): 0.00% Time (mean ± σ): 2.951 ms ± 16.854 μs ┊ GC (mean ± σ): 0.00% ± 0.00% ▃█▆▂ ▂▂▃▄▅▆█████▅▅▄▄▃▃▄▃▃▃▂▂▁▁▁▂▁▂▂▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▂▁▂▂ ▃ 2.93 ms Histogram: frequency by time 3.06 ms < Memory estimate: 0 bytes, allocs estimate: 0. ```
1 parent 749d377 commit d6018f8

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/misc_ops.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ struct ClassicalXOR{N} <: AbstractOperation
133133
bits::NTuple{N,Int}
134134
"The index of the classical bit that will store the results"
135135
store::Int
136-
function ClassicalXOR(bits, store)
137-
tbits = tuple(bits...)
138-
n = length(tbits)
139-
return new{n}(tbits, store)
140-
end
141136
end
137+
138+
ClassicalXOR(bits,store) = ClassicalXOR{length(bits)}(tuple(bits...),store)

src/pauli_frames.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ function apply!(frame::PauliFrame, xor::ClassicalXOR)
6565
end
6666
frame.measurements[f, xor.store] = value
6767
end
68+
return frame
6869
end
6970

7071
function apply!(frame::PauliFrame, op::sMX) # TODO implement a faster direct version

src/sumtypes.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct SymbolicDataType
99
types#::Core.SimpleVector
1010
fieldnames
1111
originaltype
12+
originaltype_parameterized
1213
end
1314
_header(s) = s
1415
_header(s::SymbolicDataType) = s.name
@@ -19,6 +20,8 @@ _fieldnames(s) = fieldnames(s)
1920
_fieldnames(s::SymbolicDataType) = s.fieldnames
2021
_originaltype(s) = s
2122
_originaltype(s::SymbolicDataType) = s.originaltype
23+
_originaltype_parameterized(s) = s
24+
_originaltype_parameterized(s::SymbolicDataType) = s.originaltype_parameterized
2225

2326
"""
2427
```
@@ -38,7 +41,8 @@ julia> make_variant_deconstruct(sCNOT, :apply!, (:s,))
3841
"""
3942
function make_variant_deconstruct(type::Union{DataType,SymbolicDataType}, call, preargs=(), postargs=())
4043
variant = Expr(:call, _symbol(type), _fieldnames(type)...)
41-
original = :(($(_originaltype(type)))($(_fieldnames(type)...)))
44+
original = :(($(_originaltype_parameterized(type)))($(_fieldnames(type)...)))
45+
#:($variant => begin $(Expr(:call, call, preargs..., original, postargs...)); nothing end) # useful when you are searching for type instabilities due to inconsistent output types for a method (usually also pointing to a method not following the conventions of the API)
4246
:($variant => $(Expr(:call, call, preargs..., original, postargs...)))
4347
end
4448

@@ -94,8 +98,7 @@ function make_sumtype_variant_constructor(type)
9498
if isa(type, DataType) || isa(type, SymbolicDataType)
9599
return :( CompactifiedGate(g::$(_header(type))) = CompactifiedGate'.$(_symbol(type))($([:(g.$n) for n in _fieldnames(type)]...)) )
96100
else
97-
#return :( CompactifiedGate(g::$(_header(type))) = (@warn "The operation is of a type that can not be unified, defaulting to slower runtime dispatch" typeof(g); return g) )
98-
return :()
101+
return :() # this is taken care of by a default constructor that also warns about the failure to compactify
99102
end
100103
end
101104

@@ -135,15 +138,21 @@ function make_all_sumtype_infrastructure_expr(t::DataType, callsigs)
135138
push!(concrete_types, ut) # fallback
136139
end
137140
sumtype = make_sumtype(concrete_types)
141+
@debug "compiling a total of $(length(concrete_types)) concrete types"
138142
constructors = make_sumtype_variant_constructor.(concrete_types)
139143
methods = [make_sumtype_method(concrete_types, call, preargs, postargs) for (call, preargs, postargs) in callsigs]
144+
modulename = gensym(:CompactifiedGate)
140145
return quote
146+
#module $(modulename)
147+
#using QuantumClifford
148+
#import QuantumClifford: CompactifiedGate, # todo
141149
$(concretifier_workarounds_types...)
142150
$(sumtype.args...) # defining the sum type
143151
$(constructors...) # creating constructors for the sumtype which turn our concrete types into instance of the sum type
144152
$(concretifier_additional_constructors...) # creating constructors for the newly generated "workaround" concrete types
145-
:( CompactifiedGate(g::AbstractOperation) = (@warn "The operation is of a type that can not be unified, defaulting to slower runtime dispatch" typeof(g); return g) )
153+
:(CompactifiedGate(g::AbstractOperation) = (@warn "The operation is of a type that can not be unified, defaulting to slower runtime dispatch" typeof(g); return g) )
146154
$(methods...)
155+
#end
147156
end
148157
end
149158

@@ -161,7 +170,7 @@ function concretifier(t)
161170
parameterized_type = t{typeparams...}
162171
ftypes = parameterized_type.types
163172
fnames = fieldnames(t)
164-
push!(names, SymbolicDataType(name, ftypes, fnames, t))
173+
push!(names, SymbolicDataType(name, ftypes, fnames, t, parameterized_type))
165174
push!(generated_concretetypes, :(
166175
struct $(name)
167176
$([:($n::$t) for (n,t) in zip(fnames,ftypes)]...)

0 commit comments

Comments
 (0)