-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghz.jl
65 lines (46 loc) · 1.4 KB
/
ghz.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Base.Iterators: product
using Random
@enum Color red blue
@enum Texture smooth rough
Property = Union{Color, Texture}
@enum MeasurementAction peek poke
struct Box
color::Color
texture::Texture
end
Box(red, smooth)
Triple{T} = NTuple{3, T} where T <: Any
ExperimentContext = Triple{MeasurementAction}
ExperimentResult = Triple{Property}
SystemState = Triple{Box}
function Base.rand(::Type{Box})
all_colors = instances(Color)
all_textures = instances(Texture)
color, texture = product(all_colors, all_textures) |> collect |> rand
Box(color, texture)
end
Base.rand(::Type{NTuple{3, T}}) where T = (rand(T), rand(T), rand(T))
triple_box_factory() = rand(SystemState)
function measure(box::Box, action::MeasurementAction)::Property
if action == peek
box.color
else
@assert action == poke
box.texture
end
end
function measure(state::SystemState, context::ExperimentContext)::ExperimentResult
ExperimentResult(measure(box, action) for (box, action) in zip(state, context))
end
const context1 = (poke, peek, peek)
const context2 = (poke, poke, poke)
function run_experiments(num_runs)
rng = MersenneTwister(42)
function experiment(id)
state = triple_box_factory()
context = rand(rng, Bool) ? context1 : context2
result = measure(state, context)
(context, result)
end
[experiment(id) for id in 1:num_runs]
end