Simulated optics experiment/Simulator: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎Extra "credit": Added links to the ATS example.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 4 users not shown)
Line 17:
In this task you should write the program or set of programs that simulate the experimental apparatus, so generating raw data. The ''[[Simulated optics experiment/Data analysis]]'' task handles analysis of the raw data. There should be no need for the simulator and the data analysis to be in the same programming language. Therefore any implementation of the data analysis can be used to check your implementation of the simulator.
 
Write simulations of the following experimental devices. The descriptions may seem complicated, but the [[#ObjectIcon|Object Icon]] and, [[#Python|Python]], and [[#RATFOR|RATFOR]] examples can serve as references. The formerRATFOR is acertainly littlethe simplersimplest, andbut isthe notObject fullIcon may seem more familiar to "object oriented" programmers. The two are similar. The Python reformulates some calculations as Gibbs vectors (the "arrow" kind of strangevector), and runs as multiple Iconismsprocesses.
 
* A ''light source'', which occasionally emits two pulses of [[wp:Plane_of_polarization|plane-polarized light]], one towards the left, the other towards the right. Both pulses have an amplitude of 1. About half the time the pulses are polarized, respectively, at an angle of 0° on the left and 90° on the right. The rest of the time the angles are reversed: 90° on the left, 0° on the right. A random number generator should be used to select between the two settings. If the 0° angle is on the left, then a "0" should be recorded in a log. Otherwise a "1" is recorded.
Line 29:
* On the right, the first angle is 22.5° and the second is 67.5°.
 
The simulation is run by having the light source emit some number of pulses and letting the other devices do their work. How you arrange this to work is up to you. The [[#Python|Python]] example, for instance, runs each device as a separate process, connected to each other by queues. But you can instead have, for instance, an event loop, coroutines, etc.--or even just ordinary, procedural calculation of the numbers. The last method is simplest, and perfectly correct. It is what the [[#ObjectIcon|Object Icon]] exampleand does[[#RATFOR|RATFOR]] examples do. However, surely all the methods have their place in the world of simulations.
 
The program must output its "raw data" results in the format shown here by example:
Line 639:
 
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
This is a reasonably faithful translation of Python code with however some important differences.
 
We use threads instead of processes and communication is done via channels instead of queues. Note that as Nim
is a statically typed language, channels must specify the type of data exchanged. Fortunately, in the Python code each
queue is used with a single type: either integer, vector or a dictionary. So we were able to specify the type of data
to exchange between threads.
 
As there is no way to kill a thread, we added a function to stop a mechanism (and the associated thread). We could also
have used a global termination boolean but using a function is cleaner.
 
Due to the way channels work, we had to use pointers to transmit them between threads.
 
We kept the Mechanism class (defined as an inheritable object type in Nim). We added a termination boolean and a state
for the PRNG. Indeed, the default random generator is not thread safe, so we had to manage one for each thread (i.e.
each instance of Mechanism associated to a thread).
 
<syntaxhighlight lang="Nim">import std/[math, os, random, sequtils, strutils, tables]
 
 
####################################################################################################
# Vector.
 
# A simple implementation of Gibbs vectors, suited to our purpose.
# A vector is stored in polar form, with the angle in
# degrees between 0 (inclusive) and 360 (exclusive).
type Vector = object
magnitude: float
angle: float
 
func initVector*(magnitude, angle: float): Vector =
Vector(magnitude: magnitude, angle: floorMod(angle, 360))
 
func `$`*(v: Vector): string =
"Vector($1, $2)".format(v.magnitude, v.angle)
 
func fromRect*(x, y: float): Vector =
## Return a vector for the given rectangular coordinates.
Vector(magnitude: hypot(x, y), angle: floorMod(arctan2(y, x).radToDeg, 360))
 
func toRect*(v: Vector): (float, float) =
## Return the x and y coordinates of the vector.
result[0] = v.magnitude * cos(v.angle.degToRad)
result[1] = v.magnitude * sin(v.angle.degToRad)
 
func `*`*(v: Vector; scalar: float): Vector =
## Multiply a vector by a scalar, returning a new vector.
Vector(magnitude: v.magnitude * scalar, angle: v.angle)
 
func dotProduct*(u, v: Vector): float =
## Return the dot product of two vectors.
u.magnitude * v.magnitude * cos(degToRad(u.angle - v.angle))
 
func `-`*(u, v: Vector): Vector =
## Return the difference of two vectors.
let (xu, yu) = u.toRect()
let (xv, yv) = v.toRect()
result = fromRect(xu - xv, yu - yv)
 
func projection*(u, v: Vector): Vector =
## Return the projection of vector "u" onto vector "v".
result = v * (dotProduct(u, v) / dotProduct(v, v))
 
 
####################################################################################################
# Channels.
 
type
 
DataOut = Table[string, int] # Representation of output data.
 
IntChannel = ptr Channel[int]
VectorChannel = ptr Channel[Vector]
DataChannel = ptr Channel[DataOut]
 
proc newIntChannel(): IntChannel =
cast[IntChannel](allocShared0(sizeof(Channel[int])))
 
proc newVectorChannel(): VectorChannel =
cast[VectorChannel](allocShared0(sizeof(Channel[Vector])))
 
proc newDataChannel(): DataChannel =
cast[DataChannel](allocShared0(sizeof(Channel[DataOut])))
 
 
####################################################################################################
# Mechanism.
 
# A Mechanism represents a part of the experimental apparatus.
type Mechanism = ref object of RootObj
randState: Rand # Random generator state (one per instance, so one per thread).
operating: bool # True if the mechanism is operating.
 
proc init(m: Mechanism) =
## Initialize a mechanism.
m.randState = initRand()
 
method run(m: Mechanism) {.base, gcsafe.}=
## Virtual method to override.
raise newException(CatchableError, "method without implementation override.")
 
proc start(m: Mechanism) {.thread.} =
## Run the mechanism.
m.operating = true
while m.operating:
m.run()
# A small pause of one ms to try not to overtax the computer.
sleep(1)
 
func stop(m: Mechanism) =
## Stop the mechanism.
m.operating = false
 
 
####################################################################################################
# LightSource.
 
# A LightSource occasionally emits oppositely plane-polarized
# light pulses, of fixed amplitude, polarized 90° with respect to
# each other. The pulses are represented by the vectors (1,0°) and
# (1,90°), respectively. One is emitted to the left, the other to
# the right. The probability is 1/2 that the (1,0°) pulse is emitted
# to the left.
 
# The LightSource records which pulse it emitted in which direction.
type LightSource = ref object of Mechanism
cL: VectorChannel
cR: VectorChannel
cLog: IntChannel
 
proc newLightSource(cL, cR: VectorChannel; cLog: IntChannel): LightSource =
## Create a LightSource object.
result = LightSource(cL: cL, cR: cR, cLog: cLog)
result.init()
 
method run(ls: LightSource) =
## Emit a light pulse.
let n = ls.randState.rand(1)
let val = 90 * n.toFloat
ls.cL[].send initVector(1, val)
ls.cR[].send initVector(1, 90 - val)
ls.cLog[].send(n)
 
 
####################################################################################################
# PolarizingBeamSplitter
 
# A polarizing beam splitter takes a plane-polarized light pulse
# and splits it into two plane-polarized pulses. The directions of
# polarization of the two output pulses are determined solely by the
# angular setting of the beam splitter—NOT by the angle of the
# original pulse. However, the amplitudes of the output pulses
# depend on the angular difference between the impinging light pulse
# and the beam splitter.
 
# Each beam splitter is designed to select randomly between one of
# two angle settings. It records which setting it selected (by
# putting that information into one of its output queues).
 
type PolarizingBeamSplitter = ref object of Mechanism
cS: VectorChannel
cS1: VectorChannel
cS2: VectorChannel
cLog: IntChannel
angles: array[2, float]
 
proc newPolarizingBeamSplitter(cS, cS1, cS2: VectorChannel; cLog: IntChannel;
angles: array[2, float]): PolarizingBeamSplitter =
## Create a PolarizingBeamSplitter object.
result = PolarizingBeamSplitter(cS: cS, cS1: cS1, cS2: cS2, cLog: cLog, angles: angles)
result.init()
 
method run(pbs: PolarizingBeamSplitter) =
## Split a light pulse into two pulses. One of output pulses
## may be visualized as the vector projection of the input pulse
## onto the direction vector of the beam splitter. The other
## output pulse is the difference between the input pulse and the
## first output pulse: in other words, the orthogonal component.
let angleSetting = pbs.randState.rand(1)
pbs.cLog[].send(angleSetting)
 
let angle = pbs.angles[angleSetting]
assert angle >= 0 and angle < 90
 
let v = pbs.cS[].recv()
let v1 = projection(v, initVector(1, angle))
let v2 = v - v1
 
pbs.cS1[].send(v1)
pbs.cS2[].send(v2)
 
 
####################################################################################################
# LightDetector.
 
# Our light detector is assumed to work as follows: if a
# uniformly distributed random number between 0 and 1 is less than
# or equal to the intensity (square of the amplitude) of an
# impinging light pulse, then the detector outputs a 1, meaning the
# pulse was detected. Otherwise it outputs a 0, representing the
# quiescent state of the detector.
 
type LightDetector = ref object of Mechanism
cIn: VectorChannel
cOut: IntChannel
 
proc newLightDetector(cIn: VectorChannel; cOut: IntChannel): LightDetector =
## Create a LightDetector object.
result = LightDetector(cIn: cIn, cOut: cOut)
result.init()
 
method run(ld: LightDetector) =
## When a light pulse comes in, either detect it or do not.
let pulse = ld.cIn[].recv()
let intensity = pulse.magnitude^2
ld.cOut[].send ord(ld.randState.rand(1.0) <= intensity)
 
 
####################################################################################################
# DataSynchronizer.
 
# The data synchronizer combines the raw data from the logs and
# the detector outputs, putting them into dictionaries of
# corresponding data.
 
type DataSynchronizer = ref object of Mechanism
cLogS: IntChannel
cLogL: IntChannel
cLogR: IntChannel
cDetectedL1: IntChannel
cDetectedL2: IntChannel
cDetectedR1: IntChannel
cDetectedR2: IntChannel
cDataOut: DataChannel
 
proc newDataSynchronizer(cLogS, cLogL, cLogR, cDetectedL1, cDetectedL2,
cDetectedR1, cDetectedR2: IntChannel;
cDataOut: DataChannel): DataSynchronizer =
## Create a DataSynchronizer object.
result = DataSynchronizer(cLogS: cLogs, cLogL: cLogL, cLogR: cLogR, cDetectedL1: cDetectedL1,
cDetectedL2: cDetectedL2, cDetectedR1: cDetectedR1,
cDetectedR2: cDetectedR2, cDataOut: cDataOut)
 
method run(ds: DataSynchronizer) =
## This method does the synchronizing.
ds.cDataOut[].send DataOut {"logS": ds.cLogS[].recv(),
"logL": ds.cLogL[].recv(),
"logR": ds.cLogR[].recv(),
"detectedL1": ds.cDetectedL1[].recv(),
"detectedL2": ds.cDetectedL2[].recv(),
"detectedR1": ds.cDetectedR1[].recv(),
"detectedR2": ds.cDetectedR2[].recv()}.toTable
 
proc saveRawData(filename: string; data: seq[DataOut]) =
 
proc saveData(f: File) =
f.write $data.len, '\n'
for entry in data:
for i, key in ["logS", "logL", "logR", "detectedL1",
"detectedL2", "detectedR1", "detectedR2"]:
f.write $entry[key]
f.write if i == 6: '\n' else: ' '
 
if filename != "-":
var f = open(filename, fmWrite)
f.saveData()
f.close()
else:
stdout.saveData()
 
 
when isMainModule:
 
if paramCount() notin [1, 2]:
quit "Usage: $1 NUM_PULSES [RAW_DATA_FILE]" % getAppFilename(), QuitFailure
let numPulses = paramStr(1).parseInt()
let rawDataFilename = if paramCount() == 2: paramStr(2) else: "-"
 
# Angles commonly used in actual experiments. Whatever angles you
# use have to be zero degrees or placed in Quadrant 1. This
# constraint comes with no loss of generality, because a
# polarizing beam splitter is actually a sort of rotated
# "X". Therefore its orientation can be specified by any one of
# the arms of the X. Using the Quadrant 1 arm simplifies data
# analysis.
 
const AnglesL = [0.0, 45.0]
const AnglesR = [22.5, 67.5]
assert allIt(AnglesL, it >= 0 and it < 90) and allIt(AnglesR, it >= 0 and it < 90)
 
# Channels used for communications between the threads.
const MaxSize = 100_000
var
cLogS = newIntChannel()
cLogL = newIntChannel()
cLogR = newIntChannel()
cL = newVectorChannel()
cR = newVectorChannel()
cL1 = newVectorChannel()
cL2 = newVectorChannel()
cR1 = newVectorChannel()
cR2 = newVectorChannel()
cDetectedL1 = newIntChannel()
cDetectedL2 = newIntChannel()
cDetectedR1 = newIntChannel()
cDetectedR2 = newIntChannel()
cDataOut = newDataChannel()
 
# Objects that will run in the various threads.
var
lightSource = newLightSource(cL, cR, cLogS)
splitterL = newPolarizingBeamSplitter(cL, cL1, cL2, cLogL, AnglesL)
splitterR = newPolarizingBeamSplitter(cR, cR1, cR2, cLogR, AnglesR)
detectorL1 = newLightDetector(cL1, cDetectedL1)
detectorL2 = newLightDetector(cL2, cDetectedL2)
detectorR1 = newLightDetector(cR1, cDetectedR1)
detectorR2 = newLightDetector(cR2, cDetectedR2)
sync = newDataSynchronizer(cLogS, cLogL, cLogR, cDetectedL1, cDetectedL2,
cDetectedR1, cDetectedR2, cDataOut)
 
# Open channels.
for c in [cLogs, cLogL, cLogR, cDetectedL1, cDetectedL2, cDetectedR1, cDetectedR2]:
c[].open(MaxSize)
for c in [cL, cR, cL1, cL2, cR1, cR2]:
c[].open(MaxSize)
cDataOut[].open(MaxSize)
 
# Threads.
var lightsourceThread,
splitterLThread,
splitterRThread,
detectorL1Thread,
detectorL2Thread,
detectorR1Thread,
detectorR2Thread,
syncThread: Thread[Mechanism]
 
# Start the threads.
lightsourceThread.createThread(start, Mechanism(lightSource))
splitterLThread.createThread(start, Mechanism(splitterL))
splitterRThread.createThread(start, Mechanism(splitterR))
detectorL1Thread.createThread(start, Mechanism(detectorL1))
detectorL2Thread.createThread(start, Mechanism(detectorL2))
detectorR1Thread.createThread(start, Mechanism(detectorR1))
detectorR2Thread.createThread(start, Mechanism(detectorR2))
syncThread.createThread(start, Mechanism(sync))
 
var data: seq[DataOut]
for i in 1..numPulses:
data.add cDataOut[].recv()
saveRawData(rawDataFilename, data)
 
# Stop the apparatus.
lightSource.stop()
splitterL.stop()
splitterR.stop()
detectorL1.stop()
detectorL2.stop()
detectorR1.stop()
detectorR2.stop()
sync.stop()
 
# Wait for thread terminations.
joinThread(lightsourceThread)
joinThread(splitterLThread)
joinThread(splitterRThread)
joinThread(detectorL1Thread)
joinThread(detectorL2Thread)
joinThread(detectorR1Thread)
joinThread(detectorR2Thread)
joinThread(syncThread)
 
# Close the channels.
for c in [cLogs, cLogL, cLogR, cDetectedL1, cDetectedL2, cDetectedR1, cDetectedR2]:
c[].close()
for c in [cL, cR, cL1, cL2, cR1, cR2]:
c[].close()
cDataOut[].close()
</syntaxhighlight>
 
{{out}}
Here is an example of the output of the analysis of data produced by the simulator:
<pre>
light pulse events 100000
 
correlation coefs
0.0° 22.5° -0.709952
0.0° 67.5° +0.707978
45.0° 22.5° +0.699308
45.0° 67.5° +0.707870
 
CHSH contrast +2.825108
2*sqrt(2) = nominal +2.828427
difference -0.003319
 
CHSH violation +0.825108</pre>
 
=={{header|ObjectIcon}}==
Line 916 ⟶ 1,314:
 
CHSH violation +0.819720
 
</pre>
 
=={{header|Phix}}==
See [[Simulated_optics_experiment/Data_analysis#Phix]] which includes the generation phase, and for javascript compatability has file save/load commented out.
 
=={{header|RATFOR}}==
There is a Ratfor preprocessor in C at https://sourceforge.net/p/chemoelectric/ratfor77 It outputs FORTRAN 77, which you can then compile like any FORTRAN 77 program.
 
Ratfor itself is a mix of Fortran syntax and C syntax. It was once, in a sense, the "Unix dialect of FORTRAN". (It was expanded into "Extended Fortran Language" but I do not remember what that added. I encountered EFL in System V Release 3.)
 
This program does the simulation in what might be the most efficient way possible. It uses very small constant space. However, one probably would want to use a better random number generator. And you have to recompile to use a different initial seed, or to generate a different number of lines, if you want different output.
 
<syntaxhighlight lang="ratfor">
# -*- mode: indented-text; tab-width: 2; -*-
 
# Reference:
#
# A. F. Kracklauer, ‘EPR-B correlations: non-locality or geometry?’,
# J. Nonlinear Math. Phys. 11 (Supp.) 104–109 (2004).
# https://doi.org/10.2991/jnmp.2004.11.s1.13 (Open access, CC BY-NC)
 
# Standard FORTRAN 77 provides no way to allocate space at runtime.
# One would have to make a call to C, or do some such thing, or simply
# recompile if you need a bigger array. However, it is not
# necessary. This program writes results to output as they are
# calculated.
 
# Random numbers are obtained from the DLARAN function of LAPACK,
# translated below into Ratfor. This is a multiplicative congruential
# generator, and so not especially good for doing
# simulations. Substitute something else if you plan to use this
# program for serious work :)
 
define(numlines, 100000)
 
define(angleL1, 0.0D0) # 'D' for double precision.
define(angleL2, 45.0D0)
define(angleR1, 22.5D0)
define(angleR2, 67.5D0)
 
function dlaran (iseed)
#
# DLARAN returns a random real number from a uniform (0,1)
# distribution. See
# https://netlib.org/lapack/explore-html/d4/dff/dlaran_8f_source.html
# for full details.
#
double precision dlaran
integer iseed(1:4)
integer m1, m2, m3, m4
parameter (m1 = 494, m2 = 322, m3 = 2508, m4 = 2549)
double precision one
parameter (one = 1.0d+0)
integer ipw2
double precision r
parameter (ipw2 = 4096, r = one / ipw2)
integer it1, it2, it3, it4
double precision rndout
intrinsic dble, mod
10 continue
#
# multiply the seed by the multiplier modulo 2**48
#
it4 = iseed( 4 )*m4
it3 = it4 / ipw2
it4 = it4 - ipw2*it3
it3 = it3 + iseed( 3 )*m4 + iseed( 4 )*m3
it2 = it3 / ipw2
it3 = it3 - ipw2*it2
it2 = it2 + iseed( 2 )*m4 + iseed( 3 )*m3 + iseed( 4 )*m2
it1 = it2 / ipw2
it2 = it2 - ipw2*it1
it1 = it1 + iseed( 1 )*m4 + iseed( 2 )*m3 + iseed( 3 )*m2 _
+ iseed( 4 )*m1
it1 = mod( it1, ipw2 )
#
# return updated seed
#
iseed(1) = it1
iseed(2) = it2
iseed(3) = it3
iseed(4) = it4
#
# convert 48-bit integer to a real number in the interval (0,1)
#
rndout = r*( dble( it1 )+r*( dble( it2 )+r*( dble( it3 ) _
+r*( dble( it4 ) ) ) ) )
if (rndout == 1.0d+0)
{
# If a real number has n bits of precision, and the first n bits
# of the 48-bit integer above happen to be all 1 (which will
# occur about once every 2**n calls), then DLARAN will be
# rounded to exactly 1.0. Since DLARAN is not supposed to return
# exactly 0.0 or 1.0 (and some callers of DLARAN, such as
# CLARND, depend on that), the statistically correct thing to do
# in this situation is simply to iterate again. N.B. the case
# DLARAN = 0.0 should not be possible.
goto 10
}
#
dlaran = rndout
end
 
function rndnum () # Random in (0,1). (Neither 0 nor 1 is returned.)
implicit none
double precision rndnum
double precision dlaran
integer iseed(1:4)
save iseed
data iseed / 1, 1, 1, 1 /
rndnum = dlaran (iseed)
end
 
subroutine ltsrc (logval, thetaL, thetaR) # Light source.
implicit none
integer logval
double precision thetaL, thetaR
double precision rndnum
continue
if (rndnum () < 0.5D0)
{
logval = 0
thetaL = 0.0D0
thetaR = 90.0D0
}
else
{
logval = 1
thetaL = 90.0D0
thetaR = 0.0D0
}
end
 
subroutine pbs (logval, theta, phi1, phi2, _
outC, outS) # Polarizing beam splitter.
implicit none
integer logval
double precision theta, phi1, phi2, outC, outS
double precision rndnum
double precision piv180
continue
piv180 = datan (1.0D0) / 45
#
# The sine might be negative, but we will be squaring it, anyway.
#
if (rndnum () < 0.5D0)
{
logval = 0
outC = dcos (piv180 * (theta - phi1))
outS = dsin (piv180 * (theta - phi1))
}
else
{
logval = 1
outC = dcos (piv180 * (theta - phi2))
outS = dsin (piv180 * (theta - phi2))
}
end
 
subroutine detec (inp, outp) # Light detector.
implicit none
double precision inp
integer outp
double precision rndnum
continue
if (rndnum () <= inp * inp)
outp = 1
else
outp = 0
end
 
subroutine event ()
implicit none
integer logS, logL, logR
integer pingL1, pingL2, pingR1, pingR2
double precision thetaL, thetaR
double precision valL1, valL2, valR1, valR2
continue
call ltsrc (logS, thetaL, thetaR)
call pbs (logL, thetaL, angleL1, angleL2, valL1, valL2)
call pbs (logR, thetaR, angleR1, angleR2, valR1, valR2)
call detec (valL1, pingL1)
call detec (valL2, pingL2)
call detec (valR1, pingR1)
call detec (valR2, pingR2)
write (*,'(I1, 6(X, I1))') logS, logL, logR, _
pingL1, pingL2, pingR1, pingR2
end
 
program OpSimS
implicit none
integer i
continue
#
# '(I0)' will not work in f2c, but I do not feel like writing out
# code for f2c that avoids printing leading spaces.
#
write (*,'(I0)') numlines
for (i = 0; i != numlines; i = i + 1)
call event ()
end
</syntaxhighlight>
 
{{out}}
The formatted output (of the raw data) will not work with f2c, so the program was compiled with gfortran, and analyzed by whatever analyzer my tab key came up with first.
<pre>
 
light pulse events 100000
 
correlation coefs
0.0° 22.5° -0.709578
0.0° 67.5° +0.704456
45.0° 22.5° +0.713273
45.0° 67.5° +0.717752
 
CHSH contrast +2.845060
2*sqrt(2) = nominal +2.828427
difference +0.016633
 
CHSH violation +0.845060
 
</pre>
Line 1,289 ⟶ 1,908:
 
3. Output is always saved to a file - there is no option to print it to the terminal.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "io" for File
import "os" for Process
Line 1,514 ⟶ 2,133:
<pre>
 
light pulse events 100,000
 
correlation coefs
0.0° 22.5° -0.702281
0.0° 67.5° +0.702892
45.0° 22.5° +0.696311
45.0° 67.5° +0.710296
 
CHSH contrast +2.811781
2*sqrt(2) = nominal +2.828427
difference -0.016646
 
CHSH violation +0.811781
</pre>
9,482

edits