Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Building scientific
experiments with F#
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman,...
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Mor...
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Mor...
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Mor...
A. Tcholakov, B. L. Green, A. C. Frangeskou, C. J. Stephen, P. Diggle,
J. Lishman, T. Wheeler, M. E. Newton, and G. W. Mor...
Anatomy of an experiment
PC
Output
device
Input
device
Science!
@ant_pt
• Concurrent control of multiple devices
• Cancellation
• Reporting of partial progress
• Signal processing
• Robust error...
• Concurrent control of multiple devices
• Cancellation
• Reporting of partial progress
• Signal processing
• Robust error...
• Software isn’t actually the key output
• Experiments start as prototypes
• Need path to fully-fledged application
Requir...
• National Instruments VISA
• C API + DLL import
• Typically very imperative and stateful
• Inherently Async, MailboxProce...
Command-request agent
• Module for creating MailboxProcessor agents
which process Command or Request messages
• Instrument...
Code…
Command-request agent
@ant_pt
Oscilloscope
• Essential tool in a
physics lab
• Records voltages
against time
• Has settings for
triggering, input
range,...
Code…
PicoScope agent
@ant_pt
PicoScope agent: review
• Async but still imperative
• Device is typically used in one of several modes:
streaming, block ...
Code…
PicoScope streaming
@ant_pt
@ant_pt
laser
to optical fibre
@ant_pt
laser
to optical fibre
sample
@ant_pt
laser
to optical fibre
sample
todetector
@ant_pt
laser
to optical fibre
sample
todetector
microwave oscillator
@ant_pt
laser
to optical fibre
sample
todetector
microwave oscillator
toantenna
@ant_pt
laser
to optical fibre
sample
todetector
microwave oscillator
toantenna
frequency tuning
@ant_pt
laser
to optical fibre
sample
todetector
microwave oscillator
toantenna
frequency tuning
magnet
@ant_pt
Code…
N-V-
spectrometer
@ant_pt
N-V-
spectrometer: review
• Declarative streaming API makes code
straightforward to read, write, and modify
• IObservable<...
397 GHz spectrometer
@ant_pt
397 GHz spectrometer
• Irradiate sample with
397 GHz mm-waves
• Sweep field on
superconducting
magnet system
• Measure wha...
397 GHz spectrometer
@ant_pt
Confocal microscope
@ant_pt
Confocal microscope
• 3-axis piezoelectric
scanning stage for
imaging
• Photon counting
detection
• Single molecule
sensit...
Confocal microscope
Pulse length
(1ns resolution)
Integrated
photon counts
Acquisition
parameters
System development time:...
Performance
• Hasn’t really been a problem
• PicoScope streaming acquisition copes with 32ns
sample interval (fastest avai...
Towards open source…
• Aim to publish the code under Apache 2.0
• Wrappers for several common lab instruments
• First need...
Conclusions
• F# is a great fit for instrument control thanks to
powerful Async, Reactive programming features
• Functiona...
Upcoming SlideShare
Loading in …5
×

Building scientific experiments with F#

632 views

Published on

Building scientific experiments with F#, leveraging its powerful Async and Reactive programming features.

From F# eXchange 2016.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Building scientific experiments with F#

  1. 1. 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
  2. 2. 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#
  3. 3. 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#
  4. 4. 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#
  5. 5. 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#
  6. 6. Anatomy of an experiment PC Output device Input device Science! @ant_pt
  7. 7. • Concurrent control of multiple devices • Cancellation • Reporting of partial progress • Signal processing • Robust error handling • Good performance Requirements @ant_pt
  8. 8. • 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
  9. 9. • Software isn’t actually the key output • Experiments start as prototypes • Need path to fully-fledged application Requirements (process) @ant_pt
  10. 10. • National Instruments VISA • C API + DLL import • Typically very imperative and stateful • Inherently Async, MailboxProcessor-like Instrument I/O @ant_pt
  11. 11. 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
  12. 12. Code… Command-request agent @ant_pt
  13. 13. 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
  14. 14. Code… PicoScope agent @ant_pt
  15. 15. 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
  16. 16. Code… PicoScope streaming @ant_pt
  17. 17. @ant_pt
  18. 18. laser to optical fibre @ant_pt
  19. 19. laser to optical fibre sample @ant_pt
  20. 20. laser to optical fibre sample todetector @ant_pt
  21. 21. laser to optical fibre sample todetector microwave oscillator @ant_pt
  22. 22. laser to optical fibre sample todetector microwave oscillator toantenna @ant_pt
  23. 23. laser to optical fibre sample todetector microwave oscillator toantenna frequency tuning @ant_pt
  24. 24. laser to optical fibre sample todetector microwave oscillator toantenna frequency tuning magnet @ant_pt
  25. 25. Code… N-V- spectrometer @ant_pt
  26. 26. 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
  27. 27. 397 GHz spectrometer @ant_pt
  28. 28. 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
  29. 29. 397 GHz spectrometer @ant_pt
  30. 30. Confocal microscope @ant_pt
  31. 31. Confocal microscope • 3-axis piezoelectric scanning stage for imaging • Photon counting detection • Single molecule sensitivity (for fluorescent samples) @ant_pt
  32. 32. Confocal microscope Pulse length (1ns resolution) Integrated photon counts Acquisition parameters System development time: ~9 months Oscillations represent manipulation of a qubit! @ant_pt
  33. 33. 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
  34. 34. 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
  35. 35. 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

×