## Introduction

Animals heavily rely on efficient detection of chemical cues to guide them in critical processes, such as detection of food or danger evasion. Often in nature, these cues form perplexing spatial gradients and organisms need to accurately interpret the information and navigate accordingly via a process known as chemotaxis. Even single-cell organisms are capable to chemotax, and they do so using the biased-random walk strategy (klinokinesis). According to this strategy, cells increase or decrease turning rates depending on whether they move down or up a chemical gradient, accordingly1. To implement this strategy, cells adapt to absolute levels of the stimulus, an ability that also allows them to detect small changes in the environment2,3. The mechanisms that control this relatively simple strategy had been extensively studied to the molecular details4,5.

Multicellular organisms, that harbor a neural system, use more sophisticated strategies when navigating based on chemical gradients. For example, chemotaxis of C. elegans nematodes is comprised of long periods of sinusoidal movement, termed ‘runs’, and intermittent turning events, where a bout of consecutive turns is known as a ‘pirouette’6,7,8. In their seminal work, Shimomura  et al.8 demonstrated that C. elegans worms modulate the probability to perform a pirouette based on the sign of the first derivative of the sensed stimulus8, similarly to the classical biased-random walk strategy observed in single-cell organisms. However, and unlike unicellular models, worms show a clear directional bias when exiting the pirouette: worms entering a pirouette following a run that was directed toward the target are more likely to exit the pirouette in the same goal-directed angle; Conversely, when entering a bout of turns following a run that was directed opposite to the target, then the exit angle is likely to be closer to 180°, thus reorienting the animals toward the target8,9. Later studies showed that in addition to modulating pirouette rates based on the sign of the first derivative, worms also take into account the magnitude of the derivative9,10,11. In addition, animals use a second navigation strategy in chemical gradients, termed klinotaxis12,13,14. In this strategy, animals continuously make smooth and gradual curvature corrections toward the chemical source, in a process termed in C.elegans ‘weathervane’13.

To support such complex navigation maneuvers, neural circuits perform various computations. These include adaptation and temporal integration of the sensed concentrations7,15; coding the magnitude of the change in the concentration15,16,17, bilateral coding18,19, and temporal coding7,20. To study coding principles and computations performed by individual neurons and circuits, it is useful to focus on animal models with a defined nervous system. In that respect, the C. elegans nematode offers a unique opportunity: it consists of a compact nervous system (302 neurons in total) for which a detailed wiring diagram is available21. Indeed, studies characterized worm chemotaxis behavior8,9,10,22,23,24, as well as the neural response to a variety of different stimuli25,26,27; Furthermore, recent advanced experimental systems measure neural activity in freely behaving animals, allowing to infer the neural correlates of chemotaxis behavior17,25,28,29.

Traditionally, chemosensory activity in C. elegans has been studied by presenting chemical cues in an on/off step-like manner, while simultaneously imaging activity from target neurons18,25,26,30,31,32. These step-like stimulations may approximate turbulent plumes, where signals are patchily distributed, and animals are exposed to cues that rapidly fluctuate in time and space33,34,35. However, animals, particularly small-size animals, are often found in limited and confined environments. For example, C. elegans worms are frequently recovered from rotting fruits36, which constitute a secluded and turbulent-free environment, where abrupt changes in concentrations are presumably uncommon. In such settings, stable gradients may be formed due to diffusion from bacterial microenvironments or food deteriorating signals37. These gradients are expected to be smooth and continuous due to simple spatiotemporal diffusion processes. Under these conditions, animals are likely to exploit the fine features of the gradient, such as its first derivative and its delicate spatial and temporal structure. Indeed, numerous recent studies exposed new layers of neural dynamics in response to temporally changing stimuli7,38,39,40,41.

The odorant diacetyl is one example for a signal secreted from bacteria in rotting fruits42. C. elegans worms strongly attract to this odorant, as it potentially indicates food sources. Diacetyl is sensed by two pairs of amphid sensory neurons in the worm—AWA and AWC17,22,26. Furthermore, AWA is the only neuron that expresses the diacetyl GPCR, ODR-10, which is required for chemotaxis toward low concentrations (<10 μM) of diacetyl27. Behaviorally, AWA activity has been shown to suppress turning events39, while AWC activity is correlated with reversals17. In response to an abrupt increase in diacetyl levels, AWA activity transiently increases and eventually adapts to the new concentration, in a history-dependent manner39. In contrast, AWC responds to an off step in diacetyl26. Both AWA and AWC are connected to first-layer interneurons (e.g., AIY and AIA) that control worm navigation39,43,44,45.

Here we study how individual neurons in C. elegans worms code smooth gradients, and how their neural dynamics dictate navigation strategy. Surprisingly, we find a previously uncharacterized mechanism, where AWA neurons pulse in response to smooth increasing gradients. Moreover, this pulsatile activity adapts to the magnitude of the gradient derivative, suggesting that navigating animals continuously seek steeper gradients, which correspond to trajectories better oriented toward the target. More fundamentally, we show that the orchestrated circuitry dynamics of AWA and AWC enables an efficient navigation strategy that outperforms the classical biased-random walk strategy.

## Results

### Smooth increasing gradients are coded by pulsatile activity

To systematically study neural responses to continuously changing chemical gradients, we developed a microfluidics-based system that can present C. elegans nematodes with a wide range of smooth gradient shapes (Fig. 1a, see Supplementary Note 1 for details). We used this setup to study the response of the AWA neurons to a gradient of diacetyl. AWA neurons responded with a single pulse when presented with a 1.2 mM on-step of diacetyl (Fig. 1b, c). Similar single-pulse responses to a range of on-step levels of diacetyl were observed by others as well39. The increased activity decayed back, close to basal levels, after ~1 min, and remained at these low levels for the duration of the experiment (20 min), despite the fact that the stimulus remained constantly on. However, when presented with monotonous increasing linear gradients, AWA neurons constantly pulsed throughout the experiment (Fig. 1d, e, Supplementary Movie 1). Each pulse typically lasted for several tens of seconds and was characterized by a sharp increase followed by an exponential decay (Fig. 1f).

Notably, pulsatile characteristics (e.g., amplitude, frequency, and duration) significantly varied among animals, but each individual animal maintained a typical pulsatile signature (p < 10−6, Fig. 1g, Supplementary Fig. 1). Furthermore, this pulsatile activity is cell autonomous as mutant strains defective in either neurotransmitter release46 or neuropeptide secretion47 exhibited vigorous pulsatile activity (Supplementary Fig. 2). While primarily cell autonomous, other neurons may be modulating this pulsatile activity, as the parameters depicting this activity slightly varied in the neurotransmitter and the neuropeptide defective animals (Supplementary Fig. 3).

### Pulsatile activity dictates behavioral outputs

We next asked whether freely moving worms also exhibit pulsatile activity during chemotaxis, and if so, do these pulses carry a meaningful information that dictates navigational outputs. For this, we developed an automated tracking system that enables measurement of neural activity in animals that freely-navigate through chemical gradients (Fig. 2a, see methods for details). In agreement with the results obtained using the microfluidic device, activity of AWA neurons rose and decayed even when animals moved strictly toward the chemoattractant source (and hence experienced continuously increasing concentrations only, Fig. 2b–d, Supplementary Movie 2). This neuronal activity correlated with subsequent behavioral outputs: worms continued to move forward as long as the AWA neurons were active, and in most cases, turned only after AWA activity considerably dropped (to 58%±15 std from the maximal level, Fig. 2c–e). To further understand how AWA activity modulates worm behavior, we used Chrimson39,48 to light-activate the AWA neuron. We found that in times that AWA was active, turns were significantly suppressed (p < 10−10, Χ2 test, Supplementary Fig. 4, and Supplementary Movie 3). This result is in agreement with Larch et al.39, who additionally found that immediately after AWA activation, turning probability increased above baseline. Taken together, these results demonstrate that AWA activity dictates forward movement in times that AWA is rising, and a turn once its activity decays. Thus, pulsatile activity may induce turns even in animals that sense increasing gradients only, as they move toward the chemoattractant source. Indeed, high-throughput chemotaxis assays demonstrated that animals may turn even when directed toward the target (Supplementary Fig. 5). These results suggest that pulsatile activity at the primary chemosensory neurons may directly dictate forward locomotion and turning events.

### Animals adapt to the magnitude of the first derivative

The gradient formed by a single odorant source, in a non-fluctuating environment, is typically Gaussian, and animals navigating toward this source are likely to encounter a gradient with increasing first derivatives (Supplementary Fig. 6). Previous reports demonstrated that neural activity and behavior correlate with the first derivative of the stimulus39,49. As pulsatile activity is highly variable between animals (Fig. 1g), we analyzed neural responses of individual animals while exponentially increasing the gradient first derivative over time. We found that the pulsatile activity correlates with the first derivative of the gradient: the larger the first derivative, the higher is the amplitude (r = 0.44, p < 10−4) and the shorter is the time interval between pulses (r = −0.31, p = 0.01, Supplementary Fig. 7 and Supplementary Movie 4).

However, worms facing sigmoidal gradients fail to adhere to this principle. To test whether coding of the first derivative magnitude is time invariant, we designed our fine-controlled microfluidic system to generate a sigmoidal (namely, hyperbolic tangent) gradient (Fig. 3b), where the first derivative of the concentration monotonically increases to reach its maximum value exactly at the midpoint of the gradient, after which the first derivative values symmetrically decrease (Fig. 3c). This symmetry around the gradient midpoint suggests that pulsatile activity would also be symmetric around the midpoint. Surprisingly however, we found that this is not the case: instead, the activity was significantly lower during the period after the midpoint (p < 0.002, Wilcoxon signed-rank test, Fig. 3a, and Supplementary Movie 5). Thus, although worms encounter the same first derivative values before and after the gradient’s maximal value, activity is significantly stronger in the first half of this function, suggesting that activity may actually adapt to the magnitude of the first derivative.

To verify that neural activity genuinely adapts to the first derivative, rather than simply adapting to the prolonged exposure to the stimulus, we analyzed activity while varying various parameters: the time to reach the midpoint, the absolute concentration values, and the maximal slope of the sigmoid at its midpoint (Fig. 3e). Remarkably, regardless of all these changes, AWA neurons activity significantly declined when crossing the midpoint, where the values of the first derivative began to decrease (Fig. 3f–i). Thus, the abrupt activity decline cannot be explained by the continuous exposure to the stimulus. Moreover, this activity cannot be explained by the mere first derivative of the concentration, nor by the fold change of the stimulus concentration (Supplementary Fig. 8). Together, these findings point to an intriguing principle, whereby neural activity adapts to the magnitude of the first derivative of the gradient. Interestingly, the midpoint of maximal first derivative also marks the position where the second derivative flips its sign from positive to negative (Fig. 3d). This raises the intriguing possibility that animals may actually sense the sign of the second derivative of the gradient (Supplementary Fig. 9a).

### A concerted neural dynamics underlies efficient navigation

We next studied the dynamics of AWCON, an additional diacetyl-sensing neuron26 in smooth gradients; to measure the concomitant activity of both AWA and AWCON, we generated a transgenic line expressing GCaMP in these two neuron types. In contrast to AWA neurons, it is the removal of diacetyl that elicits AWCON activity26. We therefore subjected the animals to periodic sinusoidal gradients of diacetyl (Fig. 4a). While AWA neurons exhibited the typical pulsatile activity, AWCON response was graded: it decayed during the increasing periods of the gradient, and was activated during decreasing periods of the gradient, in an anti-correlated manner such that it followed the first derivative of the gradient (ρ = −0.74, p < 10−20, Fig. 4a–c, and Supplementary Fig. 9). Furthermore, while AWA pulsatile activity was highly variable among the individual animals, AWCON response was robust, deterministic, and highly correlated across all tested animals (ρ = 0.83 ± 0.1, Fig. 4a–c, and Supplementary Fig. 9).

We next studied how the pulsatile activity propagates down the neural network to the AIY interneuron. In that respect, AIY is a key postsynaptic interneuron of AWA and AWC, whose activity correlates with forward movement, and its decreased activity mediates reversals31,44,45. For this, we generated a transgenic animal expressing GCaMP in both AWA and AIY neurons. When we imaged both neurons simultaneously, we found that the AIY neurons also pulsed, and these pulses highly correlated those observed in the AWA neurons (〈ρ〉 = 0.64, Fig. 4f, and Supplementary Fig. 10). However, AIY activity decayed significantly faster than that of AWA (t1/2 = 1.4 s vs 2.1 s, respectively, p < 10−8 Wilcoxon rank-sum test), possibly explaining why worms perform turns at times when AWA activity is well above its basal level (Fig. 2c–e). Since AIY pulsatile activity was observed in the neurite extension, it was practically impossible to differentiate between the bilateral AIYR and AIYL activities; however, several visually distinguishable instances demonstrated that AWAR and AWAL activity correlated with their ipsilateral AIY partners (Supplementary Movie 6). Notably, although AWAR and AWAL neurons are electrically coupled21, their activity is not well correlated; often, only one of the two neurons (either AWAR or AWAL) is active and pulsing, and in other cases both are active but pulse asynchronously (Fig. 4f, and Supplementary Fig. 10).

Taken together, when moving up a gradient, AWA and AIY pulsatile activity promotes forward movement, and the reduced AWCON activity negatively correlates with turning events. Conversely, when moving down the gradient, AWA activity is significantly lower (p < 0.009, Wilcoxon signed-rank test, Fig. 4d), thereby reducing prospects for forward movement, and AWCON activity rises to promote turning events17,31,44.

To understand the possible benefits of adapting to the gradient first derivative, we used simulations to compare the performance of two chemotaxis strategies that obey simple and general set of rules: (1) the well-established biased-random walk strategy which relies solely on the sign of the gradient first derivative1,8,23, and (2) the same strategy, but in addition, the animals also sense the magnitude of the first derivative and adapt to it. Strikingly, we find that the latter strategy always outperforms the classical biased-random walk strategy, as animals using this strategy always reach their target faster, regardless of the values used for the different parameters (Wilcoxon signed-rank test, p < 10−70, Fig. 5a–c and Supplementary Fig. 11). This is due to the fact that using the strategy that employs adaptation to the first derivative, animals continuously seek trajectories with larger first derivatives that are better directed toward the target (Fig. 5d). Importantly, the superior performance of the latter strategy is insensitive to the parameters chosen, as similar results are obtained for a wide range of parameters; for example, when varying the adaptation memory time, or when adding noise to the gradient sensed by the animal (Supplementary Figs. 12, 13).

Moreover, the simulations show that best performance is obtained when animals that adapt to the first derivative are also allowed to occasionally make turns, even when moving up the gradient (thus P+ > 0 in the simulations, Fig. 5b inset, and Supplementary Fig. 11; see also Supplementary Note 2 for details). We find that this holds only in cases where the probability to turn due to negative gradients is sufficiently high (Fig. 5b inset). These simulation-based understandings may be projected onto the intricate dynamics observed for AWA and AWCON: AWA pulsatile activity, which also adapts to the first derivative, inflicts turns even when the animal is well oriented toward the target. AWCON serves as a correcting mechanism, as its robust response to negative gradients promotes immediate turns17,31.

## Discussion

In this work, we report a previously unknown navigation strategy for efficient chemotaxis. This strategy relies on the orchestrated dynamics of two chemosensory neurons: AWCON and AWA. Remarkably, we found that the AWA chemosensory neurons code smooth gradients via pulsatile dynamics, and elucidated an intriguing principle, where the pulsatile activity adapts to the magnitude of the first derivative of the gradient.

Based on these findings, we propose the following model that underlies the efficient chemotaxis strategy (Fig. 6): When moving up the gradient, AWA pulses stochastically (Fig. 1d, e) thus inducing a forward run (Fig. 2a–c, and Supplementary Fig. 4). AWA also adapts to the first derivative, thus promoting a turn when the gradient begins to flatten, in search of a trajectory that is better oriented toward the target. However, turning based on the stochastic pulsatile activity of AWA comes at the risk of making erroneous turns that will draw the worm away from the source (Fig. 6). Therefore, this strategy may become advantageous only if combined with an efficient correcting mechanism. In that respect, this is exactly the role of AWCON, which responds robustly and deterministically (Fig. 4b) to promote turning events when facing decreasing concentrations17,31. Indeed, our simulations show that in the presence of a reliable corrective mechanism, occasional turns, even when navigating up the gradient, may become beneficial (Fig. 5b, inset). Moreover, a bout of turns (a pirouette) bears a corrective mechanism, as animals often exit a pirouette with an angle that is better oriented toward the target (when compared to the angle before the pirouette)8,9.

Interestingly, studies in C. elegans worms demonstrated how an interplay between two sensory neurons may have functional roles in coding environmental cues7,18. For example, the anatomically homologues ASEL and ASER gustatory neurons sense NaCl, where ASEL is stimulated by increases in NaCl concentration, and ASER is stimulated by decreases in NaCl concentration. Behaviorally, ASEL activity prolongs forward locomotion whereas ASER promotes turning events18. In an analogous manner, ASH neurons respond to an increase in the repellent odorant 2-nonanone, leading to a bout of turns, while the AWB neurons respond to a decrease in odor concentration, leading to turn suppression7. Thus, orchestrated dynamics of two sensory neurons may be a common design to efficiently integrate environmental signals before relaying the information to drive the appropriate behavioral outputs.

This efficient navigation strategy we report herein, where animals adapt to the first derivative of the gradient, joins other complex navigation strategies employed by multicellular organisms. These include a turn bias, where animals are likely to exit a pirouette better oriented toward the target8,9, and klinotaxis, where animals make gradual curvature corrections toward the target12,13,14.

Using simulations, we demonstrated that adaptation to the first derivative allows animals to perform better than the classical biased-random walk strategy (Fig. 5). Previous navigation studies also used simulations to provide a model that recapitulates the observed behavior. These type of simulations incorporated specific behavioral characteristics in order to reproduce the fine navigation features that were experimentally observed7,13,15. However, herein, we deliberately kept our simulations as general as possible as their sole purpose was to contrast between the two navigation strategies. Instead, we varied the various parameters of the model to show that regardless of their specific values, the strategy that employs the first derivative adaptation is always superior to the classical biased-random walk strategy.

We observed a large variability among individual worms in the AWA response to smooth gradients (Fig. 1g). Our findings (Fig. 2, and Supplementary Fig. 4), as well as those obtained by Larsch et al.39, indicate that AWA activity dictates behavioral outputs (e.g., runs and turns). Thus, the high variability in AWA activity may underlie, and presumably explain, at least some of the extensive behavioral variability often observed in chemotaxis assays (see for example, chemotaxis assay reported in Itskovits et al.9).

An additional type of neural variability lies in the activity of the left-right, anatomically symmetric, AWAR and AWAL neurons. Interestingly, in some of the worms, either AWAR or AWAL responded to the gradients, while in other worms both neurons responded. Furthermore, even in cases where both neurons responded, they often differed in their activation patterns (Fig. 4e, f, and Supplementary Fig 10). This differential dynamics may be particularly interesting in light of the neuroanatomical connections that these neurons make21. When analyzing the available connectome, both neurons directly connect (either by chemical synapses or via gap junctions) to the first layer of interneurons (e.g., AIY, AIA, and AIZ). However, unlike AWAL, AWAR is also synapsing onto deeper layer interneurons, namely, RIR, RIFR, and RIGR. This differential wiring may hint to variable outputs depending on the origin of the pulsing neuron (right or left). However, in our analyses, we could not systematically characterize a difference between the activation patterns of AWAR and AWAL.

In summary, here we report of an intriguing mechanism that underlies an efficient navigation strategy in chemical gradients. This mechanism is based on the principle that animals continuously seek increasing changes in the gradient by adapting to its first derivative. A handful of neurons suffice to implement this mechanism, suggesting that animals with higher brain systems may implement similar principles. Moreover, the underlying principle can be readily generalized and repurposed to support efficient navigation strategies utilized by other sensory modalities.

## Methods

### Strains used in this study

AZS163 (gpa-6::GCaMP3, pha-1::PHA-1; lite-1;pha-1) was generated by crossing PS6390 with AZS43 (lite-1(ce314); pha-1(e2123)).

AZS164 (gpa-6::GCaMP3, pha-1::PHA-1; lite-1; pha-1; unc-13(s69)) was generated by crossing BC168 with AZS163.

AZS165 (gpa-6::GCaMP3, pha-1::PHA-1; lite-1; pha-1; unc-31(e928)) was generated by crossing AZS163 with AZS68 (unc-31(e928); pha-1(e2123)).

AZS281 (gpa-6::GCaMP3, str-2::GCaMP3,;str-2::dsRed; pha-1::PHA-1; pha-1; lite-1), where GCaMP is expressed in AWA neurons and in AWCON. We first generated AZS162 (str-2::GCaMP3, str-2::dsRed pha-1::PHA-1; pha-1; lite-1) and then crossed with AZS163.

AZS256 (gpa-6::GCaMP3, mod-1::GCaMP3; pha-1::PHA-1; pha-1; lite-1) was generated by injecting both constructs into the double mutant.

CX16573 (ky5662[odr-7::Chrimson::SL2::mCherry,elt-2::mCherry]; kyIs587[gpa-6::G-CaMP2.2b, coel::dsRed])39.

All strains were grown on NGM plates seeded with overnight culture of OP 50 according to Brenner53. L4 worms were picked aside the day before the experiment so that all experiments were performed on young adult animals at 20 °C.

### Microfluidic-based system for generating smooth gradients

We developed a microfluidic-based system that allows generating a large variety of smooth temporal gradients (Fig. 1a). In this system, we control two syringe pumps (Chemyx fusion 400) using custom-made MATLAB code (Mathworks © Inc.). One pump holds a syringe with the chemical cue mixed with Rhodamine. The second pump holds a syringe filled with the diluting buffer (chemotaxis buffer (CTX)8). Importantly, Rhodamine alone did not elicit neural responses (Supplementary Fig. 14). Moreover, minute worm movements inside the microfluidic channel did not affect the observed neural responses as these displacements were uncorrelated to the pulsatile activity ($$\bar \rho = 0.05$$, Wilcoxon signed-rank test, p = 0.15, Supplementary Fig. 15). To verify accurate continuous flow of the gradient, and to avoid possible pressure buildup in the system, we used glass syringes (1000 series GASTIGHT, Hamilton).

Of note, diffusion, and possibly other fluid-flow processes in the tubing, causes a minute amount of the cue to arrive before its expected time based on calculation. This results in a neural response which may be observed up to 1 min ahead of its expected time. An example of such a case can be seen in Fig. 1d. Importantly, this is only a start-of-the-experiment effect, which does not affect the gradients to follow during the experiment. As soon as detectable levels of rhodamine enter the field of view, we can reliably quantitate them and accurately infer diacetyl concentrations at any given second (Fig. 1a).

Both syringes flow through Tygon tubing (0.02’ ID, Qosina Crop.) into a mixing chamber (of either 50 or 200 µL volume) with a small magnetic pole inside. The chamber is placed on a magnetic stirrer which ensures thorough mixing inside the chamber. The chamber output flows into a simple microfluidic device where the worm is restrained with its nose protruding to the flow channel (Fig. 1a). The microfluidic device is placed under the microscope for continuous imaging of the target neurons. Supplementary Note 1 includes a detailed description of the parameters used to generate each of the gradients presented in this study, along with the mathematical dynamical modeling of the system. In addition, the online information provides guidelines for the possible gradients one can generate using this system.

### Preparing worms and media

Prior to imaging experiments, worms were placed on empty NGM plates (w/o OP 50) for a short starvation period (30–60 min). Following a wash in CTX, we inserted the worm into the microfluidic device designed with a short and wide flowing channel (L = 5 mm, W = 0.5 mm, h = 35 μm) to flow the gradients through the tip of the nose of a constrained worm. The wide channel reduces flow resistance and thereby increases gradient accuracy.

We used two syringes as the input to the mixing chamber, a ‘Buffer’ syringe and a ‘Stimulus’ syringe (Fig. 1a). The ‘Buffer’ syringe contained CTX buffer with 0.12 µM diacetyl. The purpose of this initial basal concentration in the buffer syringe was to increase accuracy of the flowing gradients by reducing possible noise and variability due to minute flow fluctuations.

The ‘Stimulus’ syringe contained diacetyl (1.15 or 0.115 mM) diluted in the CTX buffer. To verify the accuracy of stimulus gradients we added to the ‘Stimulus’ syringe a Rhodamine dye (0.2–1 μM). Sequential imaging in the red channel (for Rhodamine) and the green channel (for GCaMP signal) provided high-temporal resolution measurements of the gradient throughout the experiment.

To further increase measurement accuracy we reduced the effect of worm movement by adding 10 mM Levamisole (Sigma, CAS Number: 16595-80-5), similarly to previous reports54. Importantly, the addition of levamisole did not affect the pulsatile activity as similar results were obtained when using un-anesthetized worms (Supplementary Fig. 16 and Supplementary Movie 7).

### Imaging single neurons

Imaging one of the pair of AWA neurons was done using an Olympus IX-83 inverted microscope equipped with a Photometrics EMCCD camera and a 40× magnification (0.95 NA) Olympus objective. A dual band filter (Chroma 59012) and a 2-leds illumination source (X-cite, Lumen Dynamics) were used to allow fast iterative imaging of both green and red channels sequentially. Hardware was controlled using Micro-Manager55. AWA activation (green) and the Rhodamine concentration (red) were each imaged at a rate of 1.4 frames/s. We then developed in-house MATLAB scripts to analyze the movies and to extract neural activity. Notably, imaging at 1.4 Hz was indeed sufficient to reliably capture the pulsatile calcium dynamics (Supplementary Fig. 17).

### Imaging several neurons simultaneously

Imaging of several neurons simultaneously was done using a Nikon AR1 + fast-scanning confocal system controlled by the Nikon NIS-elements software. We used a water-immersed 40× Nikon objective (1.15 NA) for imaging at a frame rate of 1.4 volumes/s. Pinhole opening was 1.2 Airy units and z slice jumps were ~0.7 µm. We then developed in-house MATLAB scripts to analyze the movies and to extract neural activity.

### Imaging freely behaving worms during chemotaxis

For these experiments, we developed a software package based on the Micromanager55 software suite for tracking and fluorescence imaging using a commercially available microscope setup. The code utilizes a motorized stage and a light source to track the worm while exciting and imaging its calcium sensor in 10× magnification. The code for this system, together with a detailed description of the entire system, can be found in our lab’s github repository: https://github.com/zaslab/FreelyMovingNeuronTracker. In this study, we used an Olympus IX-83 inverted microscope, with a 10× UPLASAPO objective, Lumen-Dynamics’ X-Cite light source, Prior H117 motorized stage and Photometrics Evolve 512 camera.

We always assayed young adult animals following a short starvation period (30–60 min). A single worm was placed in a 5 µL CTX drop on an NGM agar plate at a distance of 30 mm from a 1 µL drop of diacetyl (1.2 Molar). To prevent external perturbations to the gradient, we concealed the experimental arena: we first placed 1 mm PDMS spacers on two opposing edges of the agar arena, along the formed gradient, and then placed on them a 43 × 50 mm glass coverslip. The coverslip did not come in contact with the agar but was 1 mm above it and the imaged worm. Given that the diffusion constant of diacetyl is $$D \approx 9\, {\rm{mm}}^2/{\rm{s}}$$ (calculated based on its molecular mass), it should take roughly $$t = \frac{{L^2}}{D} = 30\,{\rm{s}}$$ for the gradient to stabilize once the coverslip is placed. The worm was kept in the 5 µL droplet for approximately 3 min before the droplet evaporated, giving the diacetyl gradient enough time to stabilize.

Once the CTX drop evaporated and the worm emerged out of it, we started imaging (frame rate of 2 Hz). We then analyzed the movies using custom-made MATLAB software to extract neuron activity together with the worm position in relation to the chemical source. For accurate determination of worm trajectories, and to compensate for the wide-angle head swings during movement, we smoothed worm tracks (Supplementary Fig. 18) with a smoothing spline, that uses a least-squares approach with penalization for roughness56,57.

### Analyses of high-throughput behavioral chemotaxis data

We used previously published data9 to analyze the turning rate of worms given their bearing in relation to the chemical source. In each of those experiments, approximately 100 worms were placed on one vertex of equilateral triangle with edge lengths of 4 cm, while diacetyl and dilution buffer were placed on the other two vertices. Tracking was done using the Multi Animal Tracker software suite9.

### Light-activating AWA neurons in freely moving animals

Worms expressing the Chrimson channel in the AWA neurons39 were picked at the L4 larval stage and separated into two groups. One group was picked into an NGM plate supplemented with 1 mM all trans-retinal (ATR, 100 mM stock was diluted 1:100 into E. coli OP50 prior to plate seeding). The second control group was picked to a NGM plate seeded with E. coli OP50 only. The following day, worms were randomly picked from either plate and subjected to three trials of behavioral analyses. During each trial we waited until the worm started a run and then exposed it for 10 s of green light (wavelength 545 nm, Bandwidth 25 nm, Intensity 14 mW/cm2). During these 10 s, we inspected whether the worm performed a reversal. Interval between consecutive trials for the same worm was at least 15 s. Importantly, the experimenter was blind to the worm’s group.

### Simulating chemotaxis performance

We contrasted the performance of two chemotaxis strategies: The first obeys the sign of the first derivative only, and hence follows the classical biased-random walk strategy. The second strategy implements on top of the first strategy the ability to adapt to the first derivative of the gradient. These simulations were intended to examine the possible benefits that arise from adapting to the magnitude of the experienced first derivative, rather than simulating a fully-detailed model in attempt to fit the experimental observations. We therefore simplified our model as much as possible, and included only the parameters necessary to contrast between the two strategies. Supplementary Note 2 provides a detailed description of the simulation, including an analytical solution for the case of linear gradients.

### Variability and individuality in neural responses

To analyze the variability of the pulsatile responses, a total of 92 discrete pulses were compiled from 10 different worms responding to a linear gradient (Fig. 1g depicts responses from 6 of the worms). To test whether each worm is characterized by significantly different pulse properties, we first calculated the standard deviations of different pulse parameters (namely amplitude, and pulse decay time) for each worm. We then shuffled all 92 pulses between the 10 worms, thus assigning each worm a random set of pulses, but each worm consisted with the same number of pulses it originally had. For this random set, we calculated the mean standard deviations for each of the pulse parameters and compared it to the mean standard deviations obtained for the original data. The results of this bootstrap analysis showed that the standard deviations of the random shuffles (N =1 06 in total) are significantly higher than those of the original data (p ≤ 10e−6). This analysis demonstrates that each worm responds with a characteristic pulsatile activity (which may suggest worm individuality) that is significantly different from pulses observed in other worms (population variability). Similar analysis was used to compare pulses amplitude and peak to peak time (p ≤ 10e-6, Supplementary Fig. 1).

### Plotting neural activity

Raster plots of neural activity are presented as heat maps (Figs. 1c, e, 2c, 3a, 4b, c, Supplementary Figs. 2a, b, 8a, 16). The values were normalized per each neuron (row) to range between 0–1: $$[{\rm{val}} - \min \left( {{\rm{val}}} \right)]/[\max \left( {{\rm{val}}} \right) - \min \left( {{\rm{val}}} \right)]$$

Neural activity within each worm was first normalized as described above. We then calculated the mean activity in each worm during the 2.5 min before and after the point of maximal first derivative. A non-parametric Wilcoxon signed rank test was used to compare the mean values of these two time periods (results are shown in Fig. 3).

### Pulse analyses

To extract the parameters of individual pulses, (Figs. 1f, g, 4e, Supplementary Figs. 1, 3, 7, 16), we marked them manually. When pulse amplitude normalization was required (Supplementary Fig. 7), it was done for each pulse in respect to the other pulses observed in the same worm according to$${\mathrm{normalized}}\,{\mathrm{amplitude}} = \left. {\left[ {{\mathrm{amplitude}} - \min \left( {{\mathrm{amplitude}}} \right)} \right]{\mathrm{/max}}\left( {{\mathrm{amplitude}}} \right)} \right]$$Peak to peak time was similarly normalized.

### Code availability

The code for imaging freely moving animals as well as the code for the simulations can be found in the github repository: https://github.com/zaslab/. Any additional data information is available upon request.

### Data availability

The mean fluorescence values of the AWA neuron and the measured gradients throughout the experiments are available under the Open Science Framework.