building scientific experiments with f#

Post on 10-Feb-2017

494 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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#

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#

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#

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#

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 handling

• Good performance

Requirements

@ant_pt

• 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

• Software isn’t actually the key output

• Experiments start as prototypes

• Need path to fully-fledged application

Requirements (process)

@ant_pt

• National Instruments VISA

• C API + DLL import

• Typically very imperative and stateful

• Inherently Async, MailboxProcessor-like

Instrument I/O

@ant_pt

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

Code…

Command-request agent

@ant_pt

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

Code…

PicoScope agent

@ant_pt

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

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

to d

etec

tor

@ant_pt

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator

@ant_pt

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antenna

@ant_pt

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antennafrequency tuning

@ant_pt

laser

to optical fibre

sample

to d

etec

tor

microwave oscillator to antennafrequency 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<'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

397 GHz spectrometer

@ant_pt

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

397 GHz spectrometer

@ant_pt

Confocal microscope

@ant_pt

Confocal microscope

• 3-axis piezoelectric scanning stage for imaging

• Photon counting detection

• Single molecule sensitivity (for fluorescent samples)

@ant_pt

Confocal microscope

Pulse length (1ns resolution)

Integrated photon counts

Acquisition parameters

System development time: ~9 months

Oscillations represent manipulation of a qubit!

@ant_pt

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

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

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

top related