Building scientific experiments with F#
Transcript of Building scientific experiments with F#
![Page 1: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/1.jpg)
Building scientific experiments with F#A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Morley
![Page 2: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/2.jpg)
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Morley
Building scientific experiments with F#
![Page 3: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/3.jpg)
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Morley
Building scientific experiments with F#
![Page 4: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/4.jpg)
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Morley
Building scientific experiments with F#
![Page 5: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/5.jpg)
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Morley
Building scientific experiments with F#
![Page 6: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/6.jpg)
Anatomy of an experiment
PC
Output device
Input device
Science!
@ant_pt
![Page 7: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/7.jpg)
• Concurrent control of multiple devices
• Cancellation
• Reporting of partial progress
• Signal processing
• Robust error handling
• Good performance
Requirements
@ant_pt
![Page 8: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/8.jpg)
• Concurrent control of multiple devices
• Cancellation
• Reporting of partial progress
• Signal processing
• Robust error handling
• Good performance
Requirements… and solutions
Async<'T>
IObservable<'T>
Choice<'T1,'T2> or exn?
}
}
@ant_pt
![Page 9: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/9.jpg)
• Software isn’t actually the key output
• Experiments start as prototypes
• Need path to fully-fledged application
Requirements (process)
@ant_pt
![Page 10: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/10.jpg)
• National Instruments VISA
• C API + DLL import
• Typically very imperative and stateful
• Inherently Async, MailboxProcessor-like
Instrument I/O
@ant_pt
![Page 11: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/11.jpg)
Command-request agent
• Module for creating MailboxProcessor agents which process Command or Request messages
• Instrument APIs often have large surface area
• Propagates errors to caller without stopping the agent
@ant_pt
![Page 12: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/12.jpg)
Code…
Command-request agent
@ant_pt
![Page 13: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/13.jpg)
Oscilloscope
• Essential tool in a physics lab
• Records voltages against time
• Has settings for triggering, input range, offset, etc.
Source: “Oscilloscope.” Wikipedia: The Free Encyclopaedia. Wikimedia Foundation, Inc. 20 Mar 2016. Web. 13 Apr 2016.
@ant_pt
![Page 14: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/14.jpg)
Code…
PicoScope agent
@ant_pt
![Page 15: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/15.jpg)
PicoScope agent: review
• Async but still imperative
• Device is typically used in one of several modes: streaming, block acquisition, etc.
• Functional approach:
- model acquisition configuration
- define workflow to run a general acquisition
- push sample blocks via IObservable<'T>
@ant_pt
![Page 16: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/16.jpg)
Code…
PicoScope streaming
@ant_pt
![Page 17: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/17.jpg)
@ant_pt
![Page 18: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/18.jpg)
laser
to optical fibre
@ant_pt
![Page 19: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/19.jpg)
laser
to optical fibre
sample
@ant_pt
![Page 20: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/20.jpg)
laser
to optical fibre
sample
to d
etec
tor
@ant_pt
![Page 21: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/21.jpg)
laser
to optical fibre
sample
to d
etec
tor
microwave oscillator
@ant_pt
![Page 22: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/22.jpg)
laser
to optical fibre
sample
to d
etec
tor
microwave oscillator to antenna
@ant_pt
![Page 23: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/23.jpg)
laser
to optical fibre
sample
to d
etec
tor
microwave oscillator to antennafrequency tuning
@ant_pt
![Page 24: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/24.jpg)
laser
to optical fibre
sample
to d
etec
tor
microwave oscillator to antennafrequency tuning
magnet
@ant_pt
![Page 25: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/25.jpg)
Code…
N-V- spectrometer
@ant_pt
![Page 26: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/26.jpg)
N-V- spectrometer: review
• Declarative streaming API makes code straightforward to read, write, and modify
• IObservable<'T> + FSharp.Control.Reactive offer a powerful way to implement DSP
• FSharp.Charting is great for prototyping
• Async provides robust cancellation support
• MailboxProcessor ensures sequential I/O
@ant_pt
![Page 27: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/27.jpg)
397 GHz spectrometer
@ant_pt
![Page 28: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/28.jpg)
397 GHz spectrometer
• Irradiate sample with 397 GHz mm-waves
• Sweep field on superconducting magnet system
• Measure what comes back and stream data with PicoScope
@ant_pt
![Page 29: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/29.jpg)
397 GHz spectrometer
@ant_pt
![Page 30: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/30.jpg)
Confocal microscope
@ant_pt
![Page 31: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/31.jpg)
Confocal microscope
• 3-axis piezoelectric scanning stage for imaging
• Photon counting detection
• Single molecule sensitivity (for fluorescent samples)
@ant_pt
![Page 32: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/32.jpg)
Confocal microscope
Pulse length (1ns resolution)
Integrated photon counts
Acquisition parameters
System development time: ~9 months
Oscillations represent manipulation of a qubit!
@ant_pt
![Page 33: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/33.jpg)
Performance
• Hasn’t really been a problem
• PicoScope streaming acquisition copes with 32ns sample interval (fastest available) in .fsx
• Time tagging acquisition copes with several million photon counts per second
@ant_pt
![Page 34: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/34.jpg)
Towards open source…
• Aim to publish the code under Apache 2.0
• Wrappers for several common lab instruments
• First need to:
- migrate to Paket, FAKE
- make namespaces consistent
- consolidate git branches
- write some unit tests (?)
@ant_pt
![Page 35: Building scientific experiments with F#](https://reader030.fdocuments.us/reader030/viewer/2022020410/589d3a721a28abd17d8b62a5/html5/thumbnails/35.jpg)
Conclusions
• F# is a great fit for instrument control thanks to powerful Async, Reactive programming features
• Functional paradigm + .fsx aids prototyping
• Units of measure are the cherry on top
• Working towards open source instrument libraries
@ant_pt