Neutron Spin Flipper 

Optimization using 

Active Learning and 

Fitting Algorithms

BY: SHREYA SHETE

MENTORED BY: MARKUS BLEUEL
Outline

• Background

• What is a Spin Flip and Neutron Spin Flipper?

• Why Spin Flippers are Important

• Project Overview

• Simulating a Neutron Spin Flipper

• Ideal vs. Non-Ideal Spin Flips

• Aligning a Non-Ideal Spin Flipper

• Methods 

• Conclusion

• Software/Languages

• Future Steps
Spin Flip: a 180◦ rotation of the 

polarization vector 

What is a Spin Flip and Neutron Spin Flipper?

Polarizer Analyzer Spin flipper OFF

Spin flipper ON

ON

OFF T=1

T=0

DC Coils

• 50 G

AC Coils

• 5 G @ 150 kHz

Prototype Neutron Spin Flipper
Why Neutron Spin Flippers Are Important

• The Blue Signal is the sum of the two flipper 

modulations

• SANS becomes sensitive to sample dynamics when 

the neutron beam is modulated

L2 * ꙍ2

L1 * ꙍ1

Modulated- Intensity SANS (MISANS): Ex: Normal vs. Sickle Cell Hemoglobin

Wiki: Frequency Beating
Why Neutron Spin Flippers Are Important

• The Blue Signal is the sum of the two flipper 

modulations

• SANS becomes sensitive to sample dynamics when 

the neutron beam is modulated

L2 * ꙍ2

L1 * ꙍ1

Modulated- Intensity SANS (MISANS):

Wiki: Frequency Beating

Ex: Normal vs. Sickle Cell Hemoglobin
Simulating a Neutron Spin Flipper

• If neutron spin is flipped, Transmission goes to zero (T=0), 

which is what we measure in a real experiment

• Find a proper math function (shown on the next slide)

• Ideal Test Case: When B_DC=50G, 5G@150kHz => T=0

• In reality, there are Magnetic Stray Fields that need to be 

accounted for (shown on next slide) 

• The Contour Plots focus on the RF-part of the flipper 

• AC Magnetic Field on the x-axis (3-7 G)

• Frequency of Precession on the y-axis (130-170 kHz)

• Transmission is the z-slices/contours
Ideal Spin Flip vs. Non-Ideal Spin Flips 

• Optimum would be at...

• B_AC = 5 gauss

• Frequency of Precession = 150 kHz

• Transmission=0

Optimum: (5, 150)

• Ideal Spin Flip Formula + Static Fields

• Optimum Shifts in Position

Frequency (kHz) Frequency (kHz)

Frequency (kHz) Frequency (kHz)

AC Magnetic Field (gauss) AC Magnetic Field (gauss)

AC Magnetic Field (gauss) AC Magnetic Field (gauss)

Plot from level list Plot from level list

Plot from level list Plot from level list Optimum
Aligning a Non-Ideal Spin Flipper

• Find optimum (x,y) for perfect 

flip with as few measurements 

as possible

• X: AC Magnetic Field (gauss)

• Y : Frequency of Precession (kHz)

• Z: Transmission

• Test different methods and find 

the best way to align a flipper

Optimum (x,y) 

X

Y Z
Methods

Which Method is the 

fastest

• In a well-known 

environment?

• In an unknown 

environment

Classic Grid Search

Non-Linear Iterative Curve 

Fitting

Least Squares Fitting of Data 

Points

Bayesian Optimization
Method 1. Classic Grid Search

Optimum (5.0, 141.2)

Searches all of the Polarization Values in Matrix a to find the position 

of the minimum value to calculate the optimum
1. Pick a minimization model/ method

2. Make initial guess: (5, 150) for optimum

3. Program computes model and compares 

to dataset to calculate fitting error

4. If fitting error > required fitting accuracy

• Program changes parameters and 

repeats process until either

• required fitting accuracy is 

achieved

• max iterations is reached

Method 2. Non-Linear Iterative Curve Fitting

(5.96,152.13)

Slowest

Fastest
Method 3. Least Square Fitting of Data Points

Fits 4 Points to find

• BDC_X, BDC_Y, BDC_Z

• account for the static 

fields/randomness in 

the program

• Used to calculate the 

optimum

Example with 3 Points:

AC Magnetic Field (gauss)

Frequency (kHz)
Active Learning L Hat

Brown Shoes

Round Nose

Blue Eyes

White 

Gloves

Blue 

Overalls

Green Shirt

Example: Classifying the Animated Figure 

• Reduces uncertainty in the 

model to reach optimum

• Very adaptive

Brown Mustache
Bayesian Optimization: 

Successfully Updating the 

Gaussian Process

t = 2

t = 3

t = 4

1. Find acquisition max—(max(u( λ )))

• good expected value

• high uncertainty

• max(u( λ ))= λnext

2. Use new observation (λnext ) to 

update Gaussian Process

3. Repeat ...

λ

λ

Acquisition Function (u( λ ))

acquisition max

acquisition max

acquisition max

Observation

New Observation
Method 4. Bayesian Optimization/ Active Learning

After 1 Point: After 20 Points:

Optimum Point

Current Point We’re Measuring

Previously Measured Point

Frequency (kHz)

AC Magnetic Field (gauss) AC Magnetic Field (gauss) AC Magnetic Field (gauss) AC Magnetic Field (gauss) Frequency (kHz) Frequency (kHz) Frequency (kHz)
Comparing Methods

Methods from 

Slowest to Fastest

Points 

Measured

Classic Grid Search 100

Non-Linear Iterative 

Curve Fitting

26

Bayesian 

Optimization

20

Least Square Fitting 

of Data Points

3-4

Uses Model 

Function

No Model 

Function

Adaptive Non-Linear 

Iterative Curve 

Fitting

Bayesian 

Optimization

NonAdaptiveLeast Squares 

Fitting of Data 

Points

Classic Grid 

Search
 All Methods are successful

 Classic Grid Search Method is extremely slow and inefficient. 

 Bayesian Optimization and Fitting Algorithms are modern tools 

for such a project because they are still fast while obtaining 

accurate results for the optimum. 

 Which Method is the Fastest?

 In well-known environment: Least Square Fitting of Data Points

 In unknown environment: Bayesian Optimization

Conclusion
Future Steps

Use the Red Pitaya STEMLab and Magnetic Field & Temperature 

Sensors to monitor stability of the setup to decide when to optimize

The Red Pitaya STEMLab

• Measurement Device

• Controls Both Flippers in 

all aspects

Temperature 

Sensor

Magnetic 

Field Sensor

Sensor Extension 

Module
Software/ Languages

Interactive window for application

• Python Anaconda in Jupyter Notebook

Python Libraries for Data Graphics

• Holoviews & Bokeh

Python Libraries for Optimization and Machine Learning

• SciPy & GPy (Gaussian Processes)
Acknowledgements

Special thanks to

• Markus Bleuel, my mentor

• Paul Kienzle

• Gilad Kusne

• SHIP Mentors 

• William Ratcliff, Ryan Need, Brian Maranville, Wei Zhou, 

Heather Chen-Meyer, Jacob Lamanna

• NCNR SHIP Directors (Julie Borchers, Joe Dura, Yamali Hernandez)

• Fellow SHIP students

• Center for High Resolution Neutron Scattering (CHRNS)