You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
support compactifications of more sophisticated types (#264)
Now compactification works for more types. In particular see these examples of things that previously could not be compactified:
using Revise
using QuantumClifford
using BenchmarkTools
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+2),i))
end
return circuit
end
@benchmark pftrajectories(state,circuit) setup=(state=PauliFrame(1000, 1001, 1001); circuit=x_diag_circuit_noisy_measurement(1000)) evals=1
BenchmarkTools.Trial: 1126 samples with 1 evaluation.
Range (min … max): 3.532 ms … 4.088 ms ┊ GC (min … max): 0.00% … 0.00%
Time (median): 3.573 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.577 ms ± 27.682 μs ┊ GC (mean ± σ): 0.00% ± 0.00%
▁▆▄▅▇▄▇█▆▆▃▆▄▅▅▄▂▁▂▁
▃▂▁▃▃▁▂▄▄▄▄▇█████████████████████▇▇▆▆▆▆▄▆▃▃▅▅▄▄▃▂▃▃▂▃▂▂▃▂▃ ▅
3.53 ms Histogram: frequency by time 3.63 ms <
Memory estimate: 281.25 KiB, allocs estimate: 6000.
@benchmark pftrajectories(state,circuit) setup=(state=PauliFrame(1000, 1001, 1001); circuit=compactify_circuit(x_diag_circuit_noisy_measurement(1000))) evals=1
Before:
BenchmarkTools.Trial: 53 samples with 1 evaluation.
Range (min … max): 3.495 ms … 5.890 ms ┊ GC (min … max): 0.00% … 38.90%
Time (median): 3.623 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.711 ms ± 370.663 μs ┊ GC (mean ± σ): 1.16% ± 5.34%
█▃▂ ▂▂
▄▄████▇▇██▄▇▇▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▄ ▁
3.5 ms Histogram: frequency by time 4.71 ms <
Memory estimate: 421.97 KiB, allocs estimate: 9002.
After:
BenchmarkTools.Trial: 1116 samples with 1 evaluation.
Range (min … max): 3.221 ms … 4.887 ms ┊ GC (min … max): 0.00% … 28.58%
Time (median): 3.388 ms ┊ GC (median): 0.00%
Time (mean ± σ): 3.357 ms ± 150.766 μs ┊ GC (mean ± σ): 0.36% ± 2.70%
▄█▆▂
▂▂▃▆▆▇▇▅▇▆▆▅▄▄▄▄▃▃▃▃▂▂▃▃▂▂▁▂▂▂▁▁▁▃▃▅█████▇▆▃▄▂▂▂▂▁▁▂▃▃▃▃▁▁▂ ▃
3.22 ms Histogram: frequency by time 3.49 ms <
Memory estimate: 187.50 KiB, allocs estimate: 4000.
abstract type ClassicalXORConcreteWorkaround <:AbstractOperationend# See below for more of this abomination - replace everywhere by ClassicalXOR when compactification is fixed
131
130
"""Applies an XOR gate to classical bits. Currently only implemented for functionality with pauli frames."""
$([make_variant(t) for t in concrete_types ifisa(t, DataType)]...)
58
+
$([make_variant(t) for t in concrete_types ifisa(t, DataType)||isa(t, SymbolicDataType)]...)
40
59
end
41
60
end
42
61
end
@@ -56,7 +75,8 @@ function make_sumtype_method(concrete_types, call, preargs=(), postargs=())
56
75
returnquote
57
76
function QuantumClifford.$call($(preargs...), g::CompactifiedGate, $(postargs...))
58
77
@cases g begin
59
-
$([make_variant_deconstruct(t, call, preargs, postargs) for t in concrete_types ifisa(t, DataType)]...)
78
+
$([make_variant_deconstruct(t, call, preargs, postargs) for t in concrete_types ifisa(t, DataType) ||isa(t, SymbolicDataType)]...)
79
+
#_ => @error "something wrong is happening when working with $(g) -- you are probably getting wrong results, please report this as a bug" # this being present ruins some safety guarantees, but it is useful for debugging
60
80
end
61
81
end
62
82
end
@@ -71,40 +91,89 @@ end)
71
91
```
72
92
"""
73
93
functionmake_sumtype_variant_constructor(type)
74
-
ifisa(type, DataType)
75
-
return :( CompactifiedGate(g::$(type)) = CompactifiedGate'.$(Symbol(type))($([:(g.$n) for n infieldnames(type)]...)) )
return :( CompactifiedGate(g::$(_header(type)))= CompactifiedGate'.$(_symbol(type))($([:(g.$n) for n in_fieldnames(type)]...)) )
76
96
else
77
-
return :( CompactifiedGate(g::$(type)) = (@warn"The operation is of a type that can not be unified, defaulting to slower runtime dispatch"typeof(g); return g) )
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 :()
78
99
end
79
100
end
80
101
102
+
genericsupertypeparams(t) =:body∈propertynames(t) ?genericsupertypeparams(t.body) : t
103
+
104
+
"""Returns a tuple of all concrete subtypes and all UnionAll non-abstract subtypes of a given type."""
105
+
functionget_all_subtypes(type)
106
+
if!isabstracttype(type)
107
+
ifisa(type, DataType)
108
+
isbitstype(type) ||@debug"$type will be problematic during compactification"
109
+
return [type], []
110
+
elseifisa(type, UnionAll)
111
+
return [], [type]
112
+
else
113
+
@error"The gate compiler has encountered a type that it can not handle: $type. The QuantumClifford library should continue functioning, but potentially at degraded performance. Please report this as a performance bug."
methods = [make_sumtype_method(concrete_types, call, preargs, postargs) for (call, preargs, postargs) in callsigs]
86
140
returnquote
87
-
$(sumtype.args...)
88
-
$(constructors...)
141
+
$(concretifier_workarounds_types...)
142
+
$(sumtype.args...) # defining the sum type
143
+
$(constructors...) # creating constructors for the sumtype which turn our concrete types into instance of the sum type
144
+
$(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) )
0 commit comments