Controlling spatial coherence with an optical complex medium

Control over optical spatial coherence is a key requirement in diverse applications including imaging, optical trapping, and communications. Current approaches to controlling spatial coherence are constrained by speed or limited to a single pair of optical fields. Here, we propose a method to achieve single-shot control of the spatial coherence between an arbitrary number of fields. Our method employs a multi-port linear optical device, which we realize by shaping the wavefront of the input light fields and transmitting them through a complex medium. To demonstrate the capabilities of our method, we experimentally realize a 3x3-port system and use it to generate three output beams with desired mutual correlations.


Introduction
Coherence is a fundamental notion in the science of light and offers a central degree of freedom to manipulate electromagnetic radiation of diverse physical character [1]. The concept of spatial coherence, in particular, has had a profound impact on the foundations and development of modern optics [2], and it is manifested in countless technological applications, e.g., in imaging [3,4], tomography [5][6][7], beam propagation [8], nanophotonics [9,10], trapping [11][12][13][14], and free-space optical communications [15,16]. Nevertheless, the control of spatial coherence is not yet exploited to its full extent. Researchers have developed several methods to control this coherence property, either via active devices such as spinning phase diffusers [17], spatial light modulators (SLMs) [18] and digital micromirror devices (DMDs) [19], or via passive methods such as fine tuning of the optical path [20][21][22]. Yet, all these techniques have limitations on the attainable speed or are limited to a single pair of fields.
Recently, a new class of tools to control light has emerged that exploits the properties of optical complex media when combined with wavefront shaping devices. A complex medium is an optical system that mixes the spatial and temporal degrees of freedom of an impinging field, resulting in a scrambled intensity distribution at its output [23]. The extremely large number of internal degrees of freedom of a complex medium makes the output intensity pattern disordered, yet it remains deterministic. Therefore, it is possible to fully characterize the effect of the propagation through the medium on an incident field with a linear transmission matrix (TM) [24]. Knowledge of the TM allows complex media to be used to perform a variety of tasks, once combined with programmable modulators. Applications include the control of different properties of light, e.g., intensity [25][26][27], polarization [28,29] and spectrum [30][31][32][33]. In particular, complex media have been proposed as a compact, highly-dimensional multi-port device [34], e.g., to perform quantum operations [35,36]. The use of these media in combination with wavefront shaping presents a potential alternative to other platforms, in free space [37] or integrated optics [38][39][40], which suffer from scalability issues. Interestingly, even though both random diffusers and wavefront shaping devices, such as SLMs and DMDs, have been used for the control of the spatial coherence [17][18][19], they have not been employed in combination to overcome the previous limitations.
In this work, we propose a technique to control the correlations between an arbitrary number of field pairs, based on a linear transformation applied to mutually incoherent input fields. Employing optical coherence theory, we first derive a general expression for the linear transformation that yields access to such coherence control. The linear transformation is then experimentally implemented with a complex medium in combination with SLM-based wavefront shaping. As a proof of principle, we realize a 3 × 3−port device and show that it generates any combination of mutual correlations, within the technical limitations.

Theory
In this section, after providing some background concepts, we show how to generate a set of fields with precisely controlled mutual correlations by applying an × linear transformation to a set of mutually incoherent input fields.

Basic definitions
Let the complex analytic signals 1 , . . . , represent random, statistically stationary, quasimonochromatic electric fields at different points in space. The spatial correlation between two fields and , with , ∈ {1, . . . , }, can be characterized via the mutual (or complex) degree of coherence [41] where the angle brackets stand for ensemble or time average (equivalent with ergodic and stationary fields). For each field-field pair we have that 0 ≤ | | ≤ 1, with the upper and lower limits corresponding to full coherence and full incoherence, respectively, while the intermediate values represents partial coherence. By introducing the column vector = we can collect all the degrees of coherence into an × spatial coherence matrix (abbreviated to coherence matrix from now on) where the dagger denotes conjugate transpose. The matrix K thus contains all the information about the spatial coherence in the system. The diagonal elements are given by the self degrees of coherence , which are always equal to 1, while the off-diagonal terms are the mutual degrees of coherence . The coherence matrix is also known as a statistical correlation matrix, which is a normalized covariance matrix, and must be Hermitian and positive semi-definite [42].
To characterize the overall spatial coherence of the system, we employ the measure [43] where tr stands for matrix trace. The quantity S is the distance between the coherence matrix K and the × identity matrix I, with the scaling in Eq. (3) chosen such that 0 ≤ S ≤ 1. The upper bound S = 1 is saturated exclusively when all fields are mutually fully coherent (| | = 1), hence corresponding to a spatially completely coherent system. The lower bound S = 0, on the other hand, is met only when all fields are mutually fully incoherent (| | = 0, for ≠ ), in which case the whole system is spatially completely incoherent and the coherence matrix is equal to the identity matrix (K = I).

Coherence control with linear transformation
Let us now consider a vector in that represents mutually incoherent input fields. Since in this case all mutual degrees of coherence are zero, whereupon the overall spatial coherence of the whole system is also zero, the input coherence matrix obeys K in = in † in = I. We combine the input fields via a linear transformationˆ, according to where out is the vector describing the output fields. The output coherence matrix is then given by Using the fact thatˆis deterministic and the inputs are mutually incoherent, we obtain Therefore, it is possible to generate an arbitrary output coherence matrix upon choosing a linear transformation which fulfillsˆ= where the square root is the principal square root of the matrix. The positive semi-definiteness of the coherence matrix ensures the existence of such a linear transformation [42]. We note that the assumption of mutually incoherent input fields is not necessary to control the output coherence, yet it simplifies the treatment, as indicated by Eqs. (6) and (7). We observe from Eq. (6) that under unitary transformations,ˆˆ † =ˆ †ˆ= I, the output coherence matrix always obeys K out = K in = I for an incoherent input system. The control of the output coherence thus relies on the nonunitary character of the chosen transformation. In practice, we implement the desired transformation using a multi-port linear optical device, as we describe in the following section.

Implementation
We implement a multi-port linear optical device by transmitting wavefront-shaped light through a complex medium. To use the medium to perform the desired transformation, we need to characterize its transmission matrix. To do that, we use a phase-only SLM to inject light into the medium in a well defined input basis, and we measure the output speckle pattern for each vector of the basis [24]. The number of degrees of freedom of the scattering layer (given by the number of the propagating modes supported by the material) is practically unlimited, hence the dimensionality of the TM is only limited by the number of pixels of the SLM and of the camera [44], both of which are typically on the order of one million. From the knowledge of this large fixed random matrix, we can employ the SLM in combination with the complex medium to implement a smaller but reconfigurable linear transformation [34]. Importantly, since the whole scattering matrix is too large to be fully characterized with the limited number of pixels of the SLM, all the uncontrolled modes can be considered lossy channels. These channels enable the implementation of non-unitary transformations with our compact system, comprising the SLM and the complex medium, hence allowing the control of the coherence matrix of the output fields. We note that, in integrated or standard free-space optics, realizing non-unitary transformations requires a large number of optical components, many of which are employed only to implement lossy channels [45].

Multi-port linear device
Here, we describe how to obtain a programmable linear transformation for coherence control using the system of SLM and complex medium. For illustration purposes, we focus on the simple case of a linear 3 × 3−port device, which we implement experimentally.
Let us consider three mutually incoherent light beams in 1 , in 2 and in 3 . The three nonoverlapping beams reach different regions of a phase-only SLM. For each region, segments of the SLM modulate the field locally, effectively generating spatially separated modes with controlled phase. Thus, each input in undergoes a transformationˆS LM, with dimensions × 1. Next, the three sets of modes enter the complex medium. The output intensity emanating from a complex medium typically forms a disordered interference pattern, known as speckle pattern, resulting from the mixing of the input modes. In the case under analysis, the three sets of modes entering the medium are mutually incoherent, so they do not interfere, thus leading to the sum of three independent speckle patterns. Hence, we characterize the effect of the medium with three distinct TMs, which we denote asˆC M, . The dimension of eachˆC M, is × , since they connect each set of input modes to output speckles. We want to control only three modes out of the many ones at the output. These three modes should then enclose the largest amount of the output power. Therefore, we apply a projectionˆ(of size 3 × ) to eachˆC M, to select only the output speckles we are interested in, while zeroing out the intensity of the rest.
The overall operation transforms each input beam into three outputs, according to the relation [ out 1, , out 2, , out 3, ] =ˆˆC M,ˆSLM, in , for = 1, 2, 3. We then sum the independent speckle patterns to get the final result: Since we know from Eq. (7) the target coefficients to obtain the desired output coherence matrix, we only need to invert Eq. (9) to find the transformation to be implemented with the SLM. In practice, we apply a phase conjugation, which has already been proven successful to focus light into few speckles [46]. Finally, we get the following relation: which is the configuration that we encode into the SLM to implement the desired transformation.

Experiment
In Fig. 1 we show the experimental setup, which comprises two main blocks. The first one (preparation) generates three fields characterized by a programmed coherence matrix, while the second block (verification) verifies that the encoded degrees of coherence correspond to the desired ones. We employ three different lasers as mutually incoherent inputs. The three lasers are modulated by a phase-only SLM, then they are focused onto a complex medium (ground glass diffuser) by a lens. The propagating beams are mixed by the complex medium and then collected by another lens. Through wavefront shaping, we obtain three output beams with the desired degree of coherence. The three output beams are focused by a third lens and interfere in the camera plane. A second SLM is used to characterize the degree of coherence from the interference pattern. 1 , 2 , 3 : lenses; CM: complex medium; CAM: camera.
We use two 512 × 512 pixel spatial light modulators (Meadowlark Optics P512). In the preparation stage, we use a first SLM (SLM1) to modulate three mutually incoherent input lasers (Thorlabs HRP050 and Meredith Instruments 633 nm HeNe lasers, and ≈ 650 nm FOSCO BOB-VFL650-10, see Supplement 1 for more details). Next, we focus them onto an optical complex medium (ground glass diffuser, Thorlabs DG10-1500). The SLM1 and the scattering medium together form the programmable multi-port linear device. Through wavefront shaping, the light scattered by the medium and collected by a lens forms three output beams.
In the verification stage, we use a second SLM (SLM2) to modulate the phase of the beams before a lens. This allows us to control which beam is focused onto the camera plane and to which location. From the interference patterns measured with the camera (Basler acA640-750um), we reconstruct the mutual degree of coherence. In the following sections, we describe the procedures used to encode and measure the coherence matrix of the output fields.

Preparation
The transmission matrix of the complex medium must be characterized to employ it as a part of the reconfigurable multi-port linear device. Each element of the TM connects the field modulated by the th pixel of SLM1 to the complex field of the th output mode (a camera pixel used for TM characterization). We reconstruct the TM by configuring the SLM with each vector of a complete basis of the input modes, and measuring the corresponding complex fields at the output camera. Having at our disposal phase-only SLMs, we choose the Hadamard basis, which maximizes the measured intensity and increases the signal-to-noise ratio of the reconstruction [24]. Moreover, we measure the phase of the outputs with an interference measurement, employing part of the SLM to provide a static reference field [24,47].
As discussed in Sec. 3A, the speckle patterns generated by each input field are mutually incoherent, thus they do not interfere. Therefore, we assign a different transmission matrixˆC M, to each of the three non-overlapping input beams. Each laser is spatially phase modulated by a different quadrant consisting of 256 × 256 pixels of SLM1, out of a total of 512 × 512 pixels. The outer part of each quadrant is used as a static reference for the interference measurement, while we use an area of 128 × 128 pixels (divided into 4096 square segments of 4 pixels each) to encode the Hadamard basis employed in the TM reconstruction. Once we have reconstructed the TM for each input laser, we can implement any desired linear transformation according to Eq. (10). We refer the reader to Supplement 1 for a thorough characterization of the multi-port linear device.

Verification
The multi-port linear device described above is able to encode any desired coherence matrix. To verify the correctness of the encoding, we measure each entry of the coherence matrix, that is the mutual degree of coherence of each field pair. The measurement of | | can be carried out from the relations [41] where V is the visibility, max and min are the maximum and minimum of the interference fringes, respectively, and and are the single fields' intensities. We highlight that all the quantities are defined at a single point in the camera plane, given that we can tune the relative phase of the interfering beams, as we discuss later. Moreover, even if is a complex quantity, we only consider its magnitude, as a change in the phase results in a trivial shift of the interference fringes. In the following, in writing degree of coherence we will always refer to its magnitude. In Fig. 2, we summarize the procedure employed to measure the degree of coherence. Firstly, we use SLM2 to apply a linear phase grating to two of the three output beams [48]. In the focal plane, which corresponds to the camera plane, the phase grating spatially displaces the two beams, allowing us to measure the intensity of the remaining one. In Fig. 2a and Fig. 2b, we show the intensity distribution of the first and the second beam, respectively, when the other two are displaced. We then use the phase grating to displace only one beam, and let the other two interfere, leading to the typical sinusoidal modulation across the area of the camera, as shown in Fig. 2c and Fig. 2d. In particular, Fig. 2c shows the intensity distribution when the mutual degree of coherence of the two interfering beams is low ( = 0.2), while Fig. 2d shows the case of high degree of coherence ( = 0.8). Figure 2e shows a cross-section of the interference fringes for three degrees of coherence ( = 0.2, 0.5, 0.8). The modulation depth increases for higher degrees of coherence, as expected. Next, we modulate the phase of one of the beams (termed reference phase later) from 0 to 2 . This modulation results in a spatial shift of the interference fringes. Thus, we are able to measure the visibility at each pixel of the camera. Figure 2f shows examples of the intensity at a camera pixel with respect to the reference phase for three different degrees of coherence ( = 0.2, 0.5, 0.8). From the visibility and the intensities of the single beams, we reconstruct the degree of coherence at a specific location, according to Eq. (11). As the reconstruction of the degree of coherence is noisier for regions of low intensities, we choose to only consider pixels where both the single-beam intensities are above 60% of their maximum value. We repeat the previous procedure for all the considered pixels, and average the results to obtain the reconstructed degree of coherence .
Inevitably, the encoding of a chosen coherence matrix is subject to errors, leading to a discrepancy between the encoded and the reconstructed . To minimize this discrepancy, we implement a gradient descent algorithm to optimize the multi-port linear device for minimum error (see Supplement 1 for details). The gradient descent is performed a single time to achieve better calibration of the linear port. The phase masks obtained with this final procedure can be then employed without further calibration as long as the complex medium is stable (which in the case of ground glass diffusers is only limited by the pointing stability of the lasers [34]). Figure 2g shows an example of the achieved precision in the control of the degree of coherence. Here, we report the reconstructed degrees of coherence rec , with respect to the encoded values enc . We encoded coherence matrices with identical degrees of coherence between each field pair, i.e., enc 12 = enc 13 = enc 23 , ranging from 0 to 1. The reconstructed and the encoded degrees of coherence agree to within an average error of 0.004 in the region between 0.2 and 0.8. Outside this range, we observe deviations from the expected behaviour. For low coherence, the measurement of is affected by the background noise caused by the uncontrolled modes of the complex medium, whereas for high coherence we are limited by the self-coherence of the input lasers (see Supplement 1).

Results
We now show the level of control over the coherence matrix achievable with the presented implementation. The coherence matrix is completely defined by its off-diagonal values, which are the mutual degrees of coherence of the field-field pairs. We can therefore assign to each coherence matrix a vector = [ 12 , 13 , 23 ], and visualize all the possible vectors in a space where the axes are the magnitudes of the mutual degrees of coherence. Note that the positive semi-definiteness of the coherence matrix bounds the domain of allowed vectors (see Supplement 1 for details). We discretize the three-dimensional space in a cubic grid with a step size of 0.1. In Fig. 3a, we show the experimentally achieved coherence matrices. The blue (red) dots represent the measured (encoded) vectors. We restrict ourselves to the region of degrees of coherence between 0.2 and 0.8, where the reconstructed coherence matrices do not have a large deviation with respect to the encoded ones, caused by technical limitations (see Fig. 2g and Supplement 1). To graphically make more evident the typical distance between encoded and reconstructed coherence matrices, we show in Fig. 3b one of the orthographic projections of the three-dimensional space. The blue dots represent the reconstructed vectors, while the red circles with a radius of 0.025 are centered on the encoded matrices, and provide a visual reference.
As a quantitative measure for the accuracy of our coherence matrix control scheme, we define, for each measured point, the error as the root-mean-square distance between the encoded and the reconstructed vector in the space of the coherence matrices: Here the subscript indicates the field-field pairs = {1, 2}, {1, 3}, {2, 3}. Figure 3c shows the histogram of the errors. For the majority of the coherence matrices, the error is below 0.01, which is the threshold value set in the gradient descent optimization. The mean value of the error is 0.01. Next, we characterize the statistical error associated with the measured . To do so, we repeat the previously described reconstruction procedure 10 times for each vector of the space. We then estimate the statistical error as the standard deviation of the measured ensemble. In Fig. 3d we plot the histogram of . The average statistical error is 0.008. This justifies the chosen threshold in the optimization algorithm.
Finally, we characterize the whole system of fields with a single quantity, i.e., the overall coherence. The overall coherence S is a real number ranging from 0 (full incoherence) to 1 (full coherence) and measures the coherence of the whole system, independently of how it is shared between the degrees of coherence (see Sec. 2A). In Fig. 3e, we plot the reconstructed (S rec ) versus encoded (S enc ) overall coherence, computed from all the measured vectors shown in Fig. 3a. The average error (defined as |S enc − S rec |) over all the measurements is 0.003.

Discussion
In summary, we have presented a technique to program the coherence matrix of a set of spatially separated fields through the use of a multi-port linear optical device. We have experimentally realized a 3 × 3-port system, based on wavefront shaping of mutually incoherent inputs that propagate through a complex medium. By sampling the set of allowed coherence matrices, we have shown that we can encode and successfully retrieve the majority of the matrices within an average error of 0.01. Configurations with overall coherence close to 0 and 1 are not obtainable with our implementation. Nevertheless, we point out that those are cases in which our scheme has no advantages compared to traditional approaches. In fact, for low coherence one can directly employ the input fields. For high coherence, instead, one could use a single laser and split it with beam splitters.
Remarkably, to our best knowledge this is the first time that the spatial coherence of multiple fields is controlled in a single-shot fashion. Single-shot means that, once the correct phase mask is programmed into the SLM, the spatial coherence modulation occurs after a single propagation through the system, in contrast with previous works which rely on the collection of a large ensemble of phase masks introduced by some active device [17][18][19]. Moreover, our complex medium-based device can operate on many ports. In fact, in order to increase the number of controlled fields with the same performance in terms of coherence control, we only need to keep a constant background, which allows us to retain the same minimum coherence (see Supplement 1). We can achieve this by maintaining a constant number of SLM pixels for each input laser [46], for instance by employing a larger SLM.
Our work adds an important tool to the available methods for controlling the various attributes of light. Among the various potential applications [49], our work is of considerable interest for free-space optical communications, where beams with partial spatial coherence are more robust to atmospheric turbulence compared to the coherent counterpart [15,16]. Moreover, one could use the mutual degree of coherence as a physical bit, leading to a favorable quadratic scaling of the number of communication channels with the number of input fields.

Funding. ETH Zurich Research Grant (ETH-41 19-1); Jane and Aatos Erkko Foundation; H2020 European Research Council (SMARTIES-724473).
Disclosures. The authors declare that there are no conflicts of interest related to this article.

Data availability.
The data that support the findings of this study are available from the corresponding author upon reasonable request.
Supplemental document. See Supplement 1 for supporting content.

Technical limitations
In the main text, we show that we encounter deviations between the encoded and the reconstructed degree of coherence when we try to achieve very low (approaching 0) or very high (approaching 1) values. In this section, we provide more details on the origins of these limits.
Let us start investigating the case where we want to obtain mutually incoherent outputs. According to Eq. (7) in the main text, the linear transformation we want to apply is the identity matrix, i.e., the inputs should be transmitted to the outputs unaffected. However, since it is not feasible to control all the modes supported by the scattering medium, the beams get mixed during the propagation through the medium. The resulting background noise is responsible for an unwanted contribution of each input field to every output.
We will now quantify the limitations to the minimum degree of coherence imposed by the background noise. We start considering a coherence matrix K out of the form where, for simplicity, we set the off-diagonal terms to have the same constant real value . To have mutually incoherent output fields, we want to tend to 0. From K out we extract the expression of the linear transformationˆ[Eq. (7) in the main text], which connects the mutually incoherent input fields in to the output fields out with coherence matrix K out . From the form we chose for K out [Eq. (S1)], the linear transformationˆcan be completely described by two coefficients: 11 for the diagonal terms, which are all equal, and 21 for the off-diagonal elements, which are again all equal. Note that the coefficients 11 and 21 associate the two outputs out 1 and out 2 with the single input in 1 , according to the relations out 1 = 11 in 1 , and out 2 = 21 in 1 [Eq. (8) in the main text]. Ideally, we would like | 21 | to approach zero to get zero output degree of coherence, i.e., we want | out 2 | = 0. In practice, the background noise in the output intensity pattern poses a lower bound to the intensity | out 2 | 2 , hence to | 21 |, which finally sets the minimum degree of coherence different from zero. In Fig. S1a, we show the scaling of the absolute value of the ratio | 21 / 11 | as a function of the degree of coherence . If we increase the number of inputs, i.e., the dimensionality of K out , the requirement is very similar (Fig. S1). A desired minimum degree of coherence translates into a minimum signal-to-noise ratio (SNR). In fact, considering the single input in 1 and assuming that the only contribution to out 2 is given by the background noise, | 11 | 2 is the maximum generated intensity and | 21 | 2 is the noise intensity, thus the SNR is defined as | 11 | 2 /| 21 | 2 . In Fig. S1b, we show that low coherence values demand very high SNR, which is limited by the number of SLM pixels modulating each input laser [1]. A similar argument works for a different form of K out , where the limitation is given by the element of the transformationŵ ith the minimum absolute value. Let us now consider the factors limiting the maximum degree of coherence. To investigate this case, we turn on a single input ( in 1 ), and we consider a single pair of output fields out 1 and out 2 , which are related to the input by the coefficients 11 and 21 , as discussed above. We compute the and out 2 = 21 in 1 , the degree of coherence depends on the ratio | 21 / 11 |. (b) Minimum signal-to-noise ratio (SNR) needed to encode the degree of coherence . In the case that | 21 | is only given by the background noise, the SNR is whose modulus | | is always equal to 1, regardless the values of the transformation coefficients. Nevertheless, the measurements deviate from this ideal result. To show it, we use a single input laser to generate through our system two output beams. We then let the output beams interfere and we reconstruct the degree of coherence. We report the measured interference patterns for two different input lasers in Fig. S2a and S2b. The reconstructed degrees of coherence ( 1 = 0.86 for the first input and 1 = 0.92 for the second) are lower than the ideal value of 1. This discrepancy, in line with what is reported in literature, is associated to the limited spatial coherence of the light source [2]. We show now that the maximum degree of coherence achievable with a single laser is limiting the value obtainable by the whole system. Let us consider two mutually incoherent inputs, both of them contributing to two output fields. Since the components from the different inputs do not interfere, the resulting interference pattern is given by the sum of the individual patterns. Thus, we can write the visibility in terms of the maximum max 1 , max 2 and minimum min 1 , min 2 intensity given by the contributions from the two different inputs: .

(S3)
After few algebraic passages, we get The last equation tells us that the maximum visibility obtainable by the whole system [directly linked to the degree of coherence, see Eq. (11a) in the main text] is limited by the average visibility over each single input. Therefore, the maximum degree of coherence achievable is limited by the spatial coherence of the light sources. Figure S2 shows the interference pattern when we turn on: (a) only the first input, (b) only the second one, or (c) both of them. The measured degrees of coherence resulting from the combination of the two inputs ( 12 = 0.89) is in agreement with Eq. (S5).

Mutual incoherence of the input fields
Our implementation relies on mutually incoherent inputs. To achieve this condition, we used three red lasers (Thorlabs HRP050 and Meredith Instruments 633 nm HeNe lasers, and ≈ 650 nm pen-type visual fault locator FOSCO BOB-VFL650-10), with a linewidth (HeNe ≈ 10 MHz, VFL ≈ 1 THz) much larger than the bandwidth of the employed detector (Basler acA640-750um, bandwidth ≈ 10 ÷ 100 Hz). This ensures that we can consider them mutually incoherent. To confirm it, we focused the three laser beams into a single spot, checking that no interference fringes are visible (see Fig. S3). represents the mutual degree of coherence of the two fields and .

Coherence matrix space
In this section we describe the space of the allowed coherence matrices. As discussed in the main text, the coherence matrix is Hermitian and normalized such that its diagonal elements are 1. For three field 1 , 2 and 3 , these conditions leave three degrees of freedom, which are the mutual degrees of coherence 12 , 13 and 23 . Even though the degrees of coherence are complex quantities, in this work we focus specifically on their magnitudes, as the phase corresponds only to a spatial shift of the interference fringes. Thus, for three fields, we can visualize all possible combinations of the degree of coherence magnitudes in a three-dimensional space. However, not any combination of degrees of coherence is physically acceptable. In fact, the coherence matrix must also be positive semi-definite [3]. It translates into the condition of real and positive (or zero) eigenvalues. We show in Fig. S4 the contour of the space where the positive semi-definiteness is satisfied.

Linear port
In this section we characterize the linear transformation implemented with the system of complex medium and SLM. In the main text, we focused on the specific case of a 3 × 3−port, that is a device that mixes three input fields into three output fields with controlled amplitude and phase. Thus, each input in generates three outputs according to out 1 = 1 in , out 2 = 2 in and out 3 = 3 in . Turning off two of the three inputs, we can experimentally measure the coefficients 1 , 2 and 3 . Characterizing these coefficients is the topic of the present section. We consider the input in 1 , and we measure the output intensities 1 = | 11 in 1 | 2 , 2 = | 21 in 1 | 2 and 3 = | 31 in 1 | 2 . We show an example of the resulting intensity distributions in Fig. S5a. The output beams, resulting from a speckle pattern, do not show a clean Gaussian profile. This is detrimental for the reconstruction of the degree of coherence from the interference pattern. Therefore, we introduce three small circular apertures (0.5 mm in diameter, spaced by roughly 2 mm) before the second SLM. We show in Fig. S5b the resulting spatially filtered beams. We then characterize the output intensities when we modify the encoded coefficients. Given the desired coefficients, we calculate the needed SLM maskˆS LM, according to Eq. (10) in the main text. We then increase the magnitudes of 21 and 31 from 0 to 1, keeping 11 constant and equal to 1 (Fig. S6a). We measure that 1 decreases while we increase the intensities 2 and 3 . This happens mainly because the overall power distributed in the three outputs is conserved between the transformations. Thus, if we increase the intensities of the second and third output, then 1 must decrease accordingly. We correct for this effect by characterizing the intensity ratios 2 / 1 and 3 / 1 , which are the relevant quantities for the linear port (Fig. S6b), and we use the measured characteristics to calibrate the SLM masks. We repeat the measurement 100 times (for a total time of about 30 minutes), resulting in the reported error bars, which show the maximum deviation from the mean value.
The next step is to characterize the cross-talk between the output beams. In fact, if the outputs are not completely independent, changing the intensity of one of them will affect the other two. In Fig. S6c we increase the intensity of the output 2 ( 21 from 0 to 1), while keeping 3 constant. We then repeat the measurement increasing the magnitude of 31 . We find that the fluctuations of the intensity 3 are within the error bar of 2 , which is comparable to the typical error that we report in Fig. S6b. We then conclude that the systematic cross-talk is below the statistical noise, hence not relevant.

Gradient descent optimization
In this section we describe the feedback mechanism employed to achieve the accuracy in the control of the coherence matrix reported in the main text. Mainly due to errors in the calibration of the SLM phase masks [ˆS LM, in Eq. (10) of the main text], we measure deviations between the reconstructed and the encoded degrees of coherence. Thus, we use a gradient descent algorithm to minimize the encoding errors.
Let us consider the pair of fields and . At the th iteration step, we encode the degree of coherence enc ( ), and reconstruct rec ( ). We evaluate the encoding error ( ) = rec ( ) − enc ( ) and, for the next iteration, we correct the encoded value following the relation enc ( + 1) = enc ( ) + ( ) , where is the feedback strength, that we used for all the coherence matrices. From the new values of enc ( + 1) for each pair, we construct the corrected linear port. We reiterate the process until we are satisfied with the final encoding error ( < 0.01 in our case). The gradient descent is the last step of the calibration of the setup. After running it once, we know the coefficients of the  linear port which minimize the error, and the reconstructed degree of coherence is stable over time.
We illustrate the optimization procedure in Fig. S7. We want to encode the values 12 = 0.3,