building scientific experiments with f#

35
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

Upload: antpt

Post on 10-Feb-2017

494 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Building scientific experiments with F#

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#

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#

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#

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#

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#

Anatomy of an experiment

PC

Output device

Input device

Science!

@ant_pt

Page 7: Building scientific experiments with F#

• 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#

• 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#

• 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#

• 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#

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#

Code…

Command-request agent

@ant_pt

Page 13: Building scientific experiments with F#

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#

Code…

PicoScope agent

@ant_pt

Page 15: Building scientific experiments with F#

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#

Code…

PicoScope streaming

@ant_pt

Page 17: Building scientific experiments with F#

@ant_pt

Page 18: Building scientific experiments with F#

laser

to optical fibre

@ant_pt

Page 19: Building scientific experiments with F#

laser

to optical fibre

sample

@ant_pt

Page 20: Building scientific experiments with F#

laser

to optical fibre

sample

to d

etec

tor

@ant_pt

Page 21: Building scientific experiments with F#

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator

@ant_pt

Page 22: Building scientific experiments with F#

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antenna

@ant_pt

Page 23: Building scientific experiments with F#

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antennafrequency tuning

@ant_pt

Page 24: Building scientific experiments with F#

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antennafrequency tuning

magnet

@ant_pt

Page 25: Building scientific experiments with F#

Code…

N-V- spectrometer

@ant_pt

Page 26: Building scientific experiments with F#

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#

397 GHz spectrometer

@ant_pt

Page 28: Building scientific experiments with F#

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#

397 GHz spectrometer

@ant_pt

Page 30: Building scientific experiments with F#

Confocal microscope

@ant_pt

Page 31: Building scientific experiments with F#

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#

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#

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#

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#

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