Spectral-brightness optimization of an X-ray free-electron laser by machine-learning-based tuning

Spectral-brightness optimization was performed by using a machine-learning-based beam optimizer and a new high-resolution inline spectrometer.

The key photon parameters for performing these experiments are different from experiment to experiment.For example, spectral brightness is important in spectroscopic experiments to obtain high data throughputs.In diffraction experiments, pulse energy determines the data quality as long as diffraction peaks are not blurred by bandwidth broadening.
As described below, SACLA (Ishikawa et al., 2012) has a variety of radiofrequency (RF) cavities in its injector section, and beam tuning is complicated and time consuming.This difficult beam tuning has so far relied on machine operators' skills, but recent developments in machine-tuning techniques based on machine-learning algorithms can now greatly expand the speed and flexibility of machine tuning (Duris et al., 2020).
We have implemented a machine-learning-based beam optimizer, which can be used not only for XFEL machines but also in other accelerator facilities, such as a hadron cyclotron.In this article, we apply this beam optimizer to directly maximize spectral brightness using a high-resolution inline spectrometer at SACLA.

SACLA linac
As shown in Fig. 1, there are two linear accelerators (linacs) at SACLA: an 8 GeV main linac and an 800 MeV small linac.The 8 GeV linac is used for two hard XFEL beamlines (BL2 and BL3), and also beam injection to the SPring-8 storage ring (XSBT) started in 2020.The 800 MeV linac provides electron beams for a soft X-ray FEL beamline (BL1).
Since SACLA employs a thermionic electron gun (Togawa et al., 2007) and velocity bunching in an injector section, the initial electron bunch has a relatively low current of 1 A and a long duration of 1 ns compared with an RF photo-cathode gun system.The electron beam is then longitudinally compressed to 10 fs with a peak current of 10 kA by three bunch compressors (Togawa et al., 2009).The frequencies of RF acceleration fields are increased proportionally to the bunch length, from 238 MHz, 476 MHz, L band (1428 MHz), S band (2856 MHz) to C band (5712 MHz) (Inagaki et al., 2014).To compensate for non-linearity due to the RF curvatures and bunch compressors, L-band and C-band correction cavities are installed before the first bunch compressor.In transverse planes, magnetic solenoid lenses are used to focus the electron beam in the low-energy injector section and quadrupole magnets are used after the L-band accelerator.The bunch repetition rate is 60 Hz and the accelerated bunches are distributed in three directions (BL2, BL3 and XSBT) using a kicker magnet (Hara et al., 2016).In-vacuum undulators (Kitamura, 2000) with a 18 mm period are used for FEL generation.Each undulator segment has a length of 5 m and there are 3, 17 and 19 segments installed for BL1, BL2 and BL3, respectively.

Tuning knobs
XFELs require a high-density electron beam in six-dimensional phase space.Thus, the electron bunch should be properly compressed in a longitudinal direction with minimum degradation of transverse emittance.It is also important to match the transverse beam envelope to periodic beam focusing optics of the undulator section.To ensure spatial and spectral overlap between the electron bunch and the XFEL photon pulse, undulator K values, taper and electron-beam orbit should be properly set in the undulator section.
The tuning knobs to adjust these parameters are summarized in Table 1.The longitudinal bunch compression is driven by an energy chirp applied by the off-crest acceleration, so the bunch length and peak current are optimized by the seven RF phases of 238 MHz cavity, 476 MHz cavity, L-band corrector (LBC), L-band accelerator (LB), C-band corrector (CBC), Sband accelerator (SB) and C-band accelerator (CB1-3, before the third bunch compressor).The transverse beam envelope and orbit are adjusted by the magnetic lenses, quadrupole magnets and steering magnets, as listed in Table 1.Since the beam energy is gradually reduced in the undulator section due to the FEL interaction and wakefields, the undulator K values and its taper are also tuned by the beam optimizer.
To increase the spectral brightness, the spectral bandwidth should be reduced while keeping the pulse intensity the same.The XFEL spectral bandwidth is considered to be mainly sensitive to the RF phases and undulator gaps.Since collective effects, such as coherent synchrotron radiation and wakefields, change the electron-energy chirp and affect the XFEL spectrum, the transverse optics of the electron beam are also adjusted to minimize these effects.
Because of a variety of RF cavities and a complicated injector section, there are much more tuning knobs in SACLA compared with an RF photo-cathode-based linac.On top of that, the parameters of the electron bunch, such as energy, length and peak current, should be optimized individually for three destinations.
Recent detailed demands from XFEL users, such as optimization of spectral brightness and transverse profiles, make the introduction of a machine-learning-based beam optimizer inevitable to improve the efficiency of the beamtuning process.

Spectrometer
In SACLA, a double-crystal monochromator is used to measure an averaged spectrum with a relative energy resolution (normalized by the central photon energy) of ÁE/E ' 1 Â 10 À4 eV and an inline spectrometer (Tono et al., 2013) (ISpec) is used to monitor a single-shot spectrum.The ISpec Figure 1 A schematic view of the SACLA accelerator.detects Bragg diffraction of a thin polycrystalline diamond film and its resolution is $100 eV full width at half-maximum (FWHM) at 10 keV, which is larger than a typical XFEL bandwidth of 40 eV FWHM at 10 keV.
To measure and optimize the spectral brightness of the XFEL on a single-shot basis, a high-resolution ISpec was newly introduced (Inoue et al., 2022b).Instead of a diamond film, the new ISpec employs a 0.3 mm-diameter capillary filled with 3 mm-diameter diamond microcrystals.The capillary is transversely inserted into a halo of XFEL photon pulses, and a spectral resolution of a few electronvolts can be obtained without any complicated alignments.The spectra measured by the old and new ISpecs are compared in Fig. 2.An XFEL spectral width of 25 eV FWHM was correctly obtained with the new ISpec and it is used for the spectral-brightness optimization of XFEL photon pulses.

Optimizer
A new beam optimizer is implemented based on Bayesian optimization with Gaussian process regression (GPR) (Rasmussen & Williams, 2006), one of the machine-learning methods.Hereafter, we refer to it as BO.

Gaussian process regression
GPR is a type of Bayesian regression where the probability distribution of each parameter is supposed to be Gaussian.We consider a regression model with Gaussian noise, where x is an input vector, y is an output value, h is a hyperparameter vector and is Gaussian noise.In the case of XFELs, for example, x is a tuning parameter set of the accelerator, y is the XFEL intensity and is the intrinsic fluctuation of XFEL intensity.After taking N data samples, X ðx > 1 ; . . .; x > N Þ and y ðy 1 ; . . .; y N Þ, we can estimate the output y* at an arbitrary input vector x* from the data by GPR.From the Bayes theorem, we get pð y Ã jx Ã ; y; X; hÞ ¼ pðyjX; y Ã ; x Ã ; hÞ pð y Ã jx Ã ; hÞ pðyjX; hÞ ; ð2Þ where p(y*|x*, h) is the prior distribution of the parameters, p(y|X, y*, x*, h) is the conditional probability to obtain the outputs y at the inputs X from the model function f, p( y*|x*, y, X, h) is the posterior distribution after taking the data and p(y|X, h) is the marginal likelihood of y occurring at X.In GPR, the posterior probability of a test observation, y* at x*, is Gaussian, where the contribution to the posterior probability is inversely proportional to the squared distance between the inputs of the test and each data point |x* À x n | 2 if we take a radial basis function (RBF) for the kernel.Here, the squared distance is calculated using the length-scale hyperparameter k , The shape of the function f is determined by the kernel function, observed data and the prior distribution.The hyperparameters of the model are thus the length scale for the distance calculation, the variance of the measurement noise, and the mean and variance of the prior distribution.The hyperparameters are updated after the observation of a new data sample so that the marginal likelihood is maximized.
Here, the marginal likelihood is defined as where y Ã n is defined as y* at the input vector x* = x n .In this way, the probability distribution of the model function f is updated by taking a new data sample.
For an optimization problem, the next sampling point could be intelligently selected by utilizing the expected improvement (Garnett, 2023) (EI), which is defined as an expected marginal gain compared with the best point so far, where y max is the best output value observed so far.In the BO, the next observation point is set to the maximum point of the EI for each step.

Optimizer software
The BO (Iwai et al., 2021) was implemented based on scikitlearn (Pedregosa et al., 2011) and GPyTorch (Gardner et al., 2018)/BoTorch (Balandat et al., 2020).The GPR kernel, K, is defined based on the RBF as where k and are some coefficient and noise hyperparameters, respectively (this can be represented by the convolution of the 'ConstantKernel', 'RBF' and 'WhiteKernel' with scikit-learn).With this model, hyperparameters are thus length scale, noise level, mean and variance of the prior distribution, as mentioned in Section 3.1.The given parameter ranges are normalized, treated equally in the BO, and the length scale for each parameter is limited to the range [0.075, 1.0].The length scale is a characteristic length of 'structures' in a given parameter space and the inverse number of the length scale corresponds to the number of structures to be considered in a given parameter space, because the given parameter ranges are normalized.Thus a length scale of 1 may give a smooth curve without any structures, and a small number of the length scale may give some detailed structures, but sometimes overfitting certain samples.The specified limits on length scale roughly mean just considering 1-10 structures in each parameter range, and also prevent the optimization process from overfitting certain samples.As for noise level, its initial value and range are set according to the obtained data.At the beginning of the optimization process, we obtain data, calculate the given performance index for each and evaluate the fluctuation obs , and the noise-level bounds are set to [ obs /3, obs ].For pulse-energy-related maximized optimization, the prior distribution is set to 0. After taking new sample data, the GPR model is updated to maximize the marginal likelihood.The next parameters are then derived as they give the largest EI.
The BO has been implemented so that even inexperienced operators can use it from the graphical user interface, and today it is commonly used for daily accelerator tuning.After simply specifying a template of tuning knobs, the optimization process can be started.The BO is able to handle up to 32 knobs simultaneously but it is currently limited to 16 parameters to finish the optimization process within 30 mins to an hour.With the specified tuning knobs, the optimization process starts with the noise-level evaluation as shown in Fig. 3.It usually acquires 300 samples, 10 s at 30 Hz, to evaluate noise level.Then, the BO tests some parameter sets, depending on their initial values and given parameter ranges.For example, when the initial value is placed in the middle (50%) of the parameter range, 1/4 from both ends, 25% and 75%, are tested for each parameter.Based on the first data set, a GPR model is created and a parameter set, which gives the best EI, is calculated.After applying the parameter set, the BO waits 0.5 s for RF phases and 3.0 s for quadrupole magnets depending on the response time of devices (minimum: 0.5 s).Ninety pulses, 3 s at 30 Hz, are used for the evaluation of the latest parameter set, and the GPR model is then updated.Applying the control parameters, evaluating the XFEL and updating the GPR model constitute one cycle.This cycle is usually repeated up to 100 cycles, and the optimization process takes 15-30 min depending on the number of tuning knobs.The operator can terminate the optimization at any time, either by resetting the parameters to their initial values or applying the best parameters at that moment.Fig. 4 shows an example of XFEL pulse-energy optimization using the BO.

Spectral-brightness optimization
In the spectral-brightness optimization, the new ISpec was used to maximize the spectral brightness, B avg , defined as A flowchart of the BO.

Figure 4
An example of XFEL pulse-energy optimization using the BO.
width ¼ 2; where P is the XFEL pulse energy, width denotes the spectral bandwidth and is defined as the FWHM of each spectrum, and is the scale parameter of the Cauchy distribution (the half width at half-maximum).The peak photon energy and the spectral bandwidth for each pulse were obtained by fitting the XFEL spectrum of the new ISpec to the Cauchy distribution, as shown in Fig. 2. peak is the FWHM of the distribution of observed peak photon energies of 90 pulses in a cycle; it is introduced to penalize broadening of the average spectrum due to the fluctuation of the peak photon energy.
The BO was tested with the spectral brightness of equation ( 7) as a performance index.First, the pulse energy was simply maximized, and then the BO was used to further improve the spectral brightness.The optimization began with seven RF phases and then 14 quadrupole magnets, before the undulators were used as tuning knobs.The BO increased the performance index, B avg , by a factor of 1.7.The XFEL pulse energy was maintained at a similar level before/after/during the optimization process.Fig. 5 shows the progress of the spectralbrightness optimization.As shown in Fig. 6, the FWHM of the peak photon energy was improved from 23.8 to 14.8 eV, the mean value of the single-shot spectral bandwidth (FWHM) was improved from 32.2 to 18.2 eV and the spectral brightness in averaged spectra was certainly improved by a factor of 1.7.

Conclusions
We have implemented a beam optimizer based on Bayesian optimization to adjust the machine parameters of an XFEL automatically.When the XFEL pulse energy is chosen as a performance index, the optimizer sometimes does not give the best spectral brightness.To enable spectral-brightness optimization, a high-resolution single-shot inline spectrometer has been introduced.After searching for a performance index that appropriately represents the spectral brightness, the optimizer successfully improved the spectral brightness by a factor of 1.7 and the bandwidth was reduced by half.At SACLA, the beam optimizer significantly contributes to efficient machine tuning and improvement of XFEL performance.An example of the progress of the spectral-brightness optimization with the performance index defined by equation ( 7).The optimization began with the seven RF phases and then 14 quadrupole magnets, before the undulators were used as tuning knobs.

Figure 6
The distributions of the peak photon energy (top left), the single-shot spectral bandwidth (top right) and the averaged spectra, before (black) and after (red) the spectral-brightness optimization by the BO.

Figure 2
Figure 2An example of a spectrum obtained with the existing spectrometer (black triangles) and the new spectrometer (red circles).The obtained spectra are fitted with the Cauchy distribution to evaluate spectral bandwidth (blue line).

Table 1
Tuning knobs of SACLA to be adjusted by the BO.