1 Introduction

Quantum statistical ensembles are needed in many application areas, including solving optimization problems [1, 2], training Boltzmann machines in quantum machine learning [3,4,5,6], and simulating equilibrium physics [7,8,9,10,11]. Simulating equilibrium physics is important to a wide variety of solid-state and chemistry problems including heat transfer, chemistry in solution, charge mobility, and many more. The ability to prepare arbitrary quantum statistical ensembles on quantum computers is, therefore, critical for conducting simulations in these areas.

Some of the earliest quantum algorithms for preparing mixed states include quantum walks via a metropolis algorithm [12,13,14], quantum rejection sampling [15, 16], dimension reduction [17], dynamical simulation [10, 18, 19], and variational techniques  [5,6,7,8,9,10, 20,21,22,23]. All mixed-state preparation techniques require that a non-unitary operator is applied to the system. There are several techniques for effectively applying the required non-unitary operator, including state amplification via Grover’s algorithm [14, 16, 17], quantum imaginary time evolution [10, 11], and simulating a thermal environment by coupling the system to a bath of auxiliary qubits [5, 6, 8, 15, 20, 22, 24, 25]. It is possible to avoid the use of auxiliary qubits when the simulated environment can be modeled by the environment of the quantum computer itself. In this case, some degree of control over that environment is required [26, 27].

Herein, we present a variational quantum algorithm for the preparation and optimization of mixed states on quantum computers based on a different approach. Specifically, we prepare mixed states that describe canonical ensembles of open quantum systems in thermal equilibrium with some external heat bath. The needed statistical mixtures in our algorithm are obtained through mid-circuit measurements. The entropy in our method is a function of the variational parameters in the quantum circuit. Thus, the entropy does not need to be determined from the final measurements, which can be challenging [8, 22]. Furthermore, because we do not use auxiliary qubits, our method requires the least number of qubits possible. Having direct access to the entropy and using minimal quantum resources are two main advantages of our method.

This paper is organized as follows. Section 2 presents the method we use to generate the quantum statistical ensembles. Section 3.1 presents ansatzes for generating specific values of entropy. In Sect. 4, we present a demonstration of our algorithm for a two-site Hubbard model. Section 5 provides a deeper discussion about mid-circuit measurements. Lastly, we present some concluding remarks in Sect. 6

2 Method

2.1 Problem definition

Consider a quantum system described by a Hamiltonian \(\hat{H}\) connected to an external heat bath with temperature T. The relevant thermodynamic potential for this canonical ensemble is the Helmholtz free energy

$$\begin{aligned} F(\hat{\rho }) = E(\hat{\rho }) - T S(\hat{\rho }), \end{aligned}$$
(1)

which is given by the internal energy

$$\begin{aligned} E(\hat{\rho }) = {{\,\textrm{Tr}\,}}(\hat{H} \hat{\rho }), \end{aligned}$$
(2)

and the von Neumann entropy

$$\begin{aligned} S(\hat{\rho }) = -k {{\,\textrm{Tr}\,}}(\hat{\rho }\ln \hat{\rho }), \end{aligned}$$
(3)

where \(\hat{\rho }\) is the density operator that describes the state of the system and k is the Boltzmann constant. Our goal is to find the \(\hat{\rho }\) that minimizes \(F(\hat{\rho })\) using a quantum computer.

We assume that a map from the Fock space \(\mathcal {F}\) of the quantum system to the Hilbert space \(\mathtt H^{\otimes Q}\) of a quantum register with Q qubits is provided (e.g., the Jordan–Wigner mapping). A basis for \(\mathtt H^{\otimes Q}\) is the set of states \(\{{|{n}\rangle }\}\) that is formed by all states of the form \({|{n}\rangle } = \bigotimes _{q=1}^Q {|{n_q}\rangle }\), where \(n_q \in \{0,1\}\) specifies the basis state \({|{n_q}\rangle }\) of qubit q. In this basis, a general density operator can be written as \(\hat{\rho } = \sum _{nn'} p_{nn'}{|{n}\rangle }\!{\langle {n'}|}\), where \(p_{nn'}\) are complex coefficients. Density operators can be constructed on the quantum register using a combination of quantum logic gates and mid-circuit measurements. Details of this construction are described in Sect. 3.

2.2 Approach

We need a way to create mixed states with nonzero entropy. We accomplish this by introducing mid-circuit measurements. The challenge is that the measurement of a quantum system in any pure state collapses that state to another pure state, and as all pure states have zero entropy, no single measurement induces entropy by itself. However, quantum mechanics is a probabilistic theory, where repeated measurements of the same system in the same state do not necessarily produce the same outcome. Thus, repeated measurements of the same system can produce quantum ensembles with nonzero entropy.

When we describe performing operations on a quantum computer, we are always referring to performing repeated operations on identically prepared quantum states. Let us define

$$\begin{aligned} \hat{\varrho }_0 = {|{0}\rangle }{\langle {0}|}, \end{aligned}$$
(4)

as the initial density operator of the identically prepared quantum systems. Measuring this density operator simply returns the same density operator. In order to generate entropy, we must rotate the density operator out of the \(\{{|{n}\rangle }\}\) basis. We define

$$\begin{aligned} \hat{\varrho }(\varvec{\phi }) = \hat{V}(\varvec{\phi } ) \hat{\varrho }_0 \hat{V}^{\dagger }(\varvec{\phi }), \end{aligned}$$
(5)

where \(\hat{V}(\varvec{\phi } )\) is a unitary operator and \(\varvec{\phi } \) is a collection of parameters. We assume there is a known relation \(s(\varvec{\phi } )\) such that when \(\hat{\varrho }\) is measured the resulting density operator has the entropy \(s(\varvec{\phi } )\). Let us define \(M(\hat{\varrho })\) to represent the measurement of \(\hat{\varrho }\). In principle any complete set of measurement operators can be used. In this work, we choose to perform measurements in the occupation basis \(\{{|{n}\rangle }\}\). These mid-circuit measurements then yield statistical samples of the basis states \({|{n}\rangle }\), which we use to approximate the probability distribution. After the measurement, the density operator becomes,

$$\begin{aligned} \hat{\rho }_{\text {meas}}(\varvec{\phi } ) = M[\hat{\varrho }(\varvec{\phi } )]. \end{aligned}$$
(6)

From the construction of \(\hat{V}(\varvec{\phi } )\), we have \(S(\rho _{\text {meas}}) = s(\varvec{\phi } )\). This density operator has nonzero entropy; however, it is diagonal in the \(\{{|{n}\rangle }\}\) basis. We want the density operator to be diagonal in the eigenbasis of the Hamiltonian. Thus, we perform a second unitary operation \(\hat{U}(\varvec{\theta } )\) with a second collection of parameters \(\varvec{\theta } \) and define

$$\begin{aligned} \hat{\rho }(\varvec{\phi } ,\varvec{\theta } ) = \hat{U}(\varvec{\theta } ) \hat{\rho }_{\text {meas}}(\varvec{\phi } ) \hat{U}(\varvec{\theta } ). \end{aligned}$$
(7)

Because the entropy cannot be changed by applying a unitary operator, we know that \(S(\hat{\rho }) = s(\varvec{\phi } )\). To obtain \( E( \hat{\rho })\), we use the standard measurement protocol [28] for calculating expectation values from quantum measurement samples. There are more sophisticated measuring techniques [28]. However, those techniques are beyond the scope of this work. Once both \(S(\hat{\rho })\) and \( E( \hat{\rho })\) have been obtained, \(F(\hat{\rho })\) can be calculated from Eq. (1).

The goal is to optimize both \(\varvec{\phi } \) and \(\varvec{\theta } \) in order to minimize the Helmholtz free energy. We know from statistical mechanics that the free energy is minimized by the canonical ensemble

$$\begin{aligned} \hat{\rho }^* = \frac{1}{\zeta }\sum _\lambda e^{-\beta E_\lambda } {|{\psi _\lambda }\rangle }\!{\langle {\psi _\lambda }|}, \end{aligned}$$
(8)

where \(E_\lambda \) are the eigenvalues and \({|{\psi _\lambda }\rangle }\) are the eigenstates of \(\hat{H}\), \(\lambda \) runs over the set of eigenstates, and \(\zeta \) is the partition function

$$\begin{aligned} \zeta = \sum _\lambda e^{-\beta E_\lambda }, \end{aligned}$$
(9)

where \(\beta =(kT)^{-1}\). In Appendix A, we show that there exist a \(V(\varvec{\phi } )\) and \(\hat{U}(\varvec{\theta } )\) that generate the canonical ensemble.

2.3 Algorithm

Using the approach discussed above, we can define a variational quantum algorithm for optimizing \(\varvec{\phi } \) and \(\varvec{\theta } \) so that the Helmholtz free energy (1) is minimized. The algorithm is shown in Table 1.

Table 1 Free-energy minimization algorithm

In step 1 of the algorithm, we choose initial values for \(\varvec{\phi } _0\) and \(\varvec{\theta } _0\). Certain choices could speed up the optimization and help to avoid local minima. One choice that is likely to speed up the optimization is to set \(\varvec{\phi } _0\) so that \(V(\varvec{\phi }_0)\) generates the Fermi–Dirac distribution

$$\begin{aligned} \bar{n}_q = \frac{1}{1+e^{\beta (\epsilon _q-\mu )}}, \end{aligned}$$
(10)

over the basis states \({|{n_q}\rangle }\) of the qubits where \(\epsilon _q\) are the Hartree–Fock energies and \(\mu \) is the chemical potential.

In step 3, we initialize identical copies of the quantum register. Requiring identical copies of the quantum register is not unique to our algorithm. It is required for any quantum algorithm that aims to calculate the expectation value of an operator. For any such algorithm, identical copies are required in order to acquire enough shots for a statistically accurate measurement of the expectation value. In practice, the quantum operations acting on these copies can be run sequentially instead of simultaneously, allowing us to reuse the same quantum register for each run.

In step 12, we choose a new set of parameters \(\varvec{\phi } _i'\) and \(\varvec{\theta } _i'\) and use those parameters in step 13 to compare two values of the free energy. This is the classical optimization routine that is common to quantum variational algorithms. The parameters \(\varvec{\phi } _i'\) and \(\varvec{\theta } _i'\) must be related in some way to the original parameters \(\varvec{\phi } _i\) and \(\varvec{\theta } _i\). Let \(\phi _{i\alpha }\), \(\theta _{i\alpha }\), \(\phi _{i\alpha }'\), and \(\theta _{i\alpha }'\) be parameters in the collections \(\varvec{\phi } _{i}\), \(\varvec{\theta } _{i}\), \(\varvec{\phi } _{i}'\), and \(\varvec{\theta } _{i}'\), respectively. We can relate the parameters by a small offset

$$\begin{aligned} \begin{aligned} \phi '_{i\alpha } = \phi _{i\alpha } + \Delta \phi _{i\alpha } \\ \theta '_{i\alpha } = \theta _{i\alpha } + \Delta \theta _{i\alpha }, \end{aligned} \end{aligned}$$
(11)

where \(|\Delta \phi _{i\alpha }|\) and \(|\Delta \theta _{i\alpha }|\) are small. In our demonstration below, we use the SPSA optimizer in which \(\Delta \phi _{i\alpha } = \pm \Delta \phi _{i\beta }\) and \(\Delta \theta _{i\alpha } = \pm \Delta \theta _{i\beta }\) for all \(\alpha \) and \(\beta \). The parameters in the next iteration are then chosen by taking the difference of the free energies

$$\begin{aligned} \phi _{i+1\alpha } = \phi _{i\alpha } - \gamma \frac{F(\varvec{\phi } ,\varvec{\theta } ) - F(\varvec{\phi } ',\varvec{\theta } ')}{\Delta \phi _{i\alpha }}. \end{aligned}$$
(12)

for some predefined-optimization rate \(\gamma \).

3 Entropy circuits

3.1 Discrete entropy injection via mid-circuit measurements

We want to find a unitary operator \(\hat{V}(\varvec{\phi } )\) such that when \(\hat{\varrho }= \hat{V}(\varvec{\phi } )^{\dagger }{|{0}\rangle }\!{\langle {0}|}V(\varvec{\phi } )\) is measured, the resulting density operator \(\hat{\rho }_{meas} = M(\hat{\varrho })\) has an entropy \(S(\hat{\rho }_{\text {meas}})=s(\varvec{\phi })\). Let us consider a simple example of how measurements can inject entropy into the quantum system. Consider a single qubit initialized to \(\hat{\varrho }_0 = {|{0}\rangle }\!{\langle {0}|}\). Then, apply a Hadamard operator \(\hat{\mathfrak H}=\frac{1}{\sqrt{2}}\big (\hat{\sigma }_x+\hat{\sigma }_z\big )\). This results in a density operator

$$\begin{aligned} \begin{aligned} \hat{\varrho }= \frac{1}{2}({|{0}\rangle } + {|{1}\rangle })({\langle {0}|} + {\langle {1}|}). \end{aligned} \end{aligned}$$
(13)

Next, perform the measurements

$$\begin{aligned} \begin{aligned} \hat{\rho }_{\text {meas}} = \frac{1}{2} ({|{0}\rangle }{\langle {0}|}+{|{1}\rangle }{\langle {1}|}). \end{aligned} \end{aligned}$$
(14)

The entropy of the initial state is zero

$$\begin{aligned} S(\hat{\varrho }_0) = 0, \end{aligned}$$
(15)

while the entropy of the measured state is

$$\begin{aligned} S(\hat{\rho }_{\text {meas}}) = k\ln 2. \end{aligned}$$
(16)

If instead of a single qubit, we have a set of Q qubits and we apply Hadamard operators to \(Q_H\) of them, then the entropy becomes

$$\begin{aligned} S(\hat{\rho }_{\text {meas}}) = Q_Hk\ln 2. \end{aligned}$$
(17)

Thus, we can define

$$\begin{aligned} \hat{V}^{\text {disc}}(\phi _0) = \prod _{i=0}^{\lfloor \frac{\phi _0}{\ln {2}}\rfloor } \hat{\mathfrak H}_i, \end{aligned}$$
(18)

where \(\phi _0\) is a parameter, \(\lfloor ~\rfloor \) denotes the floor function, and \(\hat{\mathfrak H}_i\) is the Hadamard operator acting on qubit i. This works as long as we only need to inject discrete amounts of entropy.

3.2 Continuous entropy injection via mid-circuit measurements

Fig. 1
figure 1

The value of \(s(\varvec{\phi }) \) given input \(\phi \) and \(Q_H\). The straight lines from bottom to top have 0, 1, 2, and 3 \(\hat{\mathfrak H}_i\) operators and no \( \hat{R}_i\) operators. The curves from bottom to top have 0, 1, and 2 \(\hat{\mathfrak H}_i\) operators and 1 \(\hat{R}_i\) operator

Fig. 2
figure 2

Depiction of the circuit model we use in our demonstration. \(\hat{V}(\varvec{\phi } )\) is composed of all of the operators before the initial measurement and \(\hat{U}(\varvec{\theta } )\) is composed of all of the operators after the initial measurement. For \(\hat{U}(\varvec{\theta } )\), we take the hardware efficient ansatz. In the figure, \(\hat{V}(\varvec{\phi } )\) is composed of a Hadamard operator on the first qubit and a rotation on the second qubit; however, the optimizer is free to replace the Hadamard operator with a rotation on the first qubit if it removes the rotation from the second qubit. The rotation in \(\hat{V}(\varvec{\phi } )\) is decomposed into native operators \(\hat{R}_i(\phi ) = \hat{R}^y_i(2\phi ) \hat{Z}\), where \(\hat{R}^y_i(\phi )\) is a rotation about the y-axis of the block sphere for qubit i and \(\hat{Z}\) is the Pauli-z matrix. The two qubit operators are controlled-NOT gates

The construction in Sect. 3.1 is only able to generate entropy that is an integer multiple of \(k\ln 2\). In general, we want to be able to generate any amount of entropy from \(S_{\text {min}} = 0\) to \(S_{\text {max}} = N k \ln 2\), where N is the total number of qubits. This can be accomplished by applying a continuous rotation to one of the qubits. Let us define the operator

$$\begin{aligned} \hat{R}_{i}(\phi ) = \hat{Z}_i \hat{R}^y_i(2\phi ) = \hat{Z}_i \cos (\phi ) + \hat{X}_i \sin (\phi ), \end{aligned}$$
(19)

where \(\hat{Z}_i\) and \(\hat{X}_i\) are the Pauli-z and Pauli-x matrices acting on qubit i, \(\hat{R}^y_i\) is the rotation operator around the y-axis of the Bloch sphere, and \(\phi \) is an arbitrary angle. The \(\hat{Z}_i\) operator is used so that we can access the Hadamard operator, \(\hat{R}_i(\pi /4) = \mathfrak h_i\). Applying this operator to \(\hat{\varrho }_0= {|{0}\rangle }\!{\langle {0}|}\) gives us

$$\begin{aligned} \begin{aligned} \hat{\varrho }&= \hat{R}_{0}(\phi )\hat{\varrho }_0 \hat{R}_{0}(\phi )\\&= \big [\cos (\phi ){|{0}\rangle }+\sin (\phi ){|{1}\rangle }\big ]\big [\cos (\phi ){\langle {0}|} + \sin (\phi ){\langle {1}|}\big ]. \end{aligned} \end{aligned}$$
(20)

Measuring generates an entropy

$$\begin{aligned} \begin{aligned} S[M(\hat{\varrho })] = - k \cos ^2(\phi ) \ln \left[ \cos ^2(\phi )\right] - k \sin ^2(\phi ) \ln \left[ \sin ^2(\phi )\right] , \end{aligned} \end{aligned}$$
(21)

that has a minimum of 0 and a maximum of \(k\ln 2\). Therefore, we can generate an arbitrary amount of entropy by applying \(Q_H=\lfloor \phi _0/\ln {2} \rfloor \) Hadamard operators to the first \(Q_H\) qubits and a \( \hat{R}_i\) operator to the next qubit. We can apply these operators by setting \(\hat{V}(\varvec{\phi } )\) to

$$\begin{aligned} \hat{V}(\varvec{\phi } ) = \hat{V}^{\text {disc}}(\phi _0) R_{Q_H + 1}\left( \phi \right) , \end{aligned}$$
(22)

where \(\varvec{\phi }\) contains only a single parameter \(\phi _0\) and we define \(\phi = \phi _0 - Q_H\) for convenience. The entropy is then given by

$$\begin{aligned} \begin{aligned} s(\varvec{\phi } )/k =- \cos ^2(\phi ) \ln \left[ \cos ^2(\phi )\right] - \sin ^2(\phi ) \ln \left[ \sin ^2(\phi )\right] + Q_H \ln (2). \end{aligned} \end{aligned}$$
(23)

Figure 1 shows plots of \(s(\varvec{\phi })\) for various combinations of Hadamard and rotation operations. We see that any input entropy can be generated from this form of \(V(\varvec{\phi })\). It should be noted that when \(\hat{V}(\varvec{\phi })\) is in this simple form, it does not need to be implemented on the quantum computer, see Appendix B for details.

3.3 Arbitrary distributions via mid-circuit measurements

Generating arbitrary entropy is not enough to generate an arbitrary density operator. Two density operators with the same entropy can have different eigenvalues. The eigenvalues of a density operator cannot be changed by applying unitary operators. Therefore, a scheme to prepare a density operator with an arbitrary distribution of eigenvalues is required to solve for the minimum free energy in the most general case.

Preparing a density operator with arbitrary eigenvalues requires a \(\hat{V}(\varvec{\phi } )\) composed of \(2^N\) operators, in general. In order to avoid an exponential number of operators, \(\hat{V}(\varvec{\phi } )\) has to be tailored to the specific problem. Bespoke operators are a common feature of variational quantum algorithms. See Appendix C for an example \(\hat{V}(\varvec{\phi } )\) that balances complexity with control over the eigenvalues.

The entropy generated by measuring the density operator after applying an arbitrary \(\hat{V}(\varvec{\phi } )\) may not be easily calculable. Fortunately, we do not have to calculate the entropy by hand. Instead, we can use the results of the mid-circuit measurements to obtain the entropy. The measurements provide us with a list of states \(\{{|{n}\rangle }\}\) and the number of shots that were measured to be in each state \(\{P_n\}\). While it is difficult to recover the entropy of the incoming ensemble, the entropy of the outgoing ensemble can be approximated using the results of the mid-circuit measurements. After the mid-circuit measurements, we approximate the density operator as \(\hat{\rho }\approx \sum _n P_n/N_s{|{n}\rangle }\!{\langle {n}|}\), where \(N_S\) is the total number of shots. Therefore, the entropy is

$$\begin{aligned} S \approx -\sum _n \frac{P_n}{N_S} \ln \frac{P_n}{N_S}, \end{aligned}$$
(24)

where the sum runs over only those states for which a shot is collected. The approximation becomes an equality for an infinite number of shots.

The number of shots required for an accurate approximation of S depends on the spread of the eigenvalues in the density operator. The distribution of eigenvalues in the canonical ensemble is controlled by the temperature. For both low \(kT<< E_{\text {max}}\) and high \(kT>> E_{\text {max}}\) temperatures, where \(E_{\text {max}}\) is the width of the energy spectrum, the specific distribution of the eigenvalues of the density operator does not have a significant effect on the free energy. For low temperature, the majority of the weight is on the ground state. For high temperature, the weight tends toward an even distribution among all states. Therefore, we can use the form of \(\hat{V}(\varvec{\phi } )\) in Sect. 3.2 for both the low and high entropy regimes. In the intermediate-temperature regime, we may need a more involved \(\hat{V}(\varvec{\phi } )\) in which there is not a simple relation \(s(\varvec{\phi })\) for the entropy. Because the weights in the canonical ensemble (8) exponentially decay with energy, we expect to get a good approximation for the entropy using Eq. (24) with a subexponential number of shots, even in the intermediate regime.

3.4 Algorithm complexity

The overall complexity of the algorithm depends on many factors. For one, it depends on the circuit depth of \(\hat{V}(\phi )\). As we have shown above, a full covering of the entropy can be achieved with a \(V(\phi )\) that requires only Q gates. Further detail can be found in Appendix C.

The complexity also depends on the required depth of \(\hat{U}(\phi )\) and on the number of optimization iterations required for the free energy to converge. These requirements are open problems. However, it is generally believed that quantum variational algorithms require low depth circuits and can be optimized efficiently [29].

Finally, the complexity depends on the number shots required to achieve an accurate sampling of the probability distribution during both the mid-circuit and final measurements. The number of shots required to achieve an accuracy of \(\epsilon \) goes as \(N_s = \mathcal {O}( 1/{\epsilon ^2})\) [29]. Notice that the \(\ln \) function in the definition of the entropy Eq. (3) means that a direct approximation of the entropy Eq. (24) can require high accuracy for some cases. However, we argue in Sect. 3.3 that we can use exact expressions for the entropy in most cases. We further argue that, for the cases where these expressions are not available, the entropy is well approximated without high accuracy.

4 Demonstrating the algorithm

Fig. 3
figure 3

Simulation of the free-energy algorithm. a shows the free energy as a function of optimization iterations. The blue curve is the simulated data and the orange line is the true minimum of the free energy. b shows the difference between the true minimum of the free energy and the simulated result as a function of temperature. The blue line with the blue dots shows results from the continuous-entropy method described in Sect. 3.2, where we use only one \(\hat{R}_i(\phi )\) operator. The blue dots show the data points the blue line is plotted to guide the eye. The green line with the green triangles shows the results from the arbitrary-distribution method described in Appendix. B, where an \(\hat{R}_i(\phi _i)\) operator is applied to each qubit. The green triangles show the data points and the green line is plotted to guide the eye. c shows the difference between the true minimum of the free energy and the simulated result for different numbers of layers of the ansatz. In a and b, \(N_{\varvec{\theta } } = 4\) and in a and c, \(kT=3.3~t\)

We demonstrate our algorithm for the two-orbital Hubbard model described by the Hamiltonian

$$\begin{aligned} H_{\text {Hubbard}} = \mu (\hat{n}_0 + \hat{n}_1) + t (c^{\dagger }_0 c_1 + c^{\dagger }_1 c_0) + u \hat{n}_0\hat{n}_1, \end{aligned}$$
(25)

where \(c^{\dagger }_i\) and \(c_i\) are creation and destruction operators, respectively, \(\hat{n}_i = c^{\dagger }_ic_i\) is the number operator, \(\mu \) is the chemical potential, t is the kinetic energy, and u is the fermion interaction strength. We scale all of the parameters by t. We set \(\mu = -3.7t\) and \(u = 0.7 t\). We use the Jordan–Wigner transformation to convert Eq. (25) to a spin model that can be mapped to the quantum register.

Figure 2 shows the circuit model. Unless otherwise stated, we choose \(\hat{V}(\varvec{\phi } )\) to be a list of Hadamard operators and one \(\hat{R}_i(\phi )\) operator, as described in Eq. (19), and we take \(\hat{U}(\varvec{\theta } )\) to be the hardware efficient ansatz. For \(\hat{V}(\varvec{\phi } )\), we start with no Hadamard operators and a rotation operator on the first qubit. If the angle of the rotation operator reaches \(\phi =\pi /4\) then a Hadamard operator is added to the first qubit, the rotation operator is moved to the second qubit, and the angle is reset to \(\phi = 0\). The entropy associated with \(\phi \) is determined by the relationship \(s(\varvec{\phi } )\) from Eq (23). We are free to choose the number of angles in \(\varvec{\theta } \), which we define as \(2N_{\varvec{\theta } }\) so that \(N_{\varvec{\theta } }\) counts the number of layers in \(\hat{U}\).

Fig. 4
figure 4

Run of the free-energy algorithm on IBM-Q Lagos. a the free energy as a function of temperature. The orange curve is the true minimum of the free energy, the large blue data points are from classical simulation, and the small pink data points are taken from the quantum computer. For the quantum data, we use the optimized angles and entropy from the classical simulation. b the difference between the free energy measured on the quantum device and the free energy from the simulation. In both a and b, \(N_{\varvec{\theta } }=4\)

In Fig. 3, we show the results of a noise-free classical simulation of our algorithm. In Fig. 3a, we plot the free energy as a function of the optimization iterations. We observe an initial fast optimization for about 20 iterations then a second slow optimization with convergence around 600 iterations. In Fig. 3b, we plot the energy difference between the true-minimum free energy and the results of our simulated algorithm. The simulation is most accurate for small and large values of T. The decrease in accuracy in the intermediate-temperature regime is expected, as discussed in Sect. 3.3. Even in this intermediate regime, the free energy from the simulation gets well within \(10\%\) of the true minimum of the free energy. Also, as predicted in Sect. 3.3, the free energy in the intermediate regime is more accurate when using the balanced ansatz described in Appendix C than when using the continuous-entropy ansatz described in Sect. 3.2. The accuracy of the algorithm is also dependent on \(N_{\varvec{\theta } }\). Figure 3c shows the free energy as a function of \(N_{\varvec{\theta } }\). We see that the final result improves as \(N_{\varvec{\theta } }\) is increased from 1 to 4. At \(N_{\varvec{\theta } }=5\) and beyond, the algorithm does not improve.

We use the optimized parameter values from the classical simulation to test the circuit on a quantum computer. Figure 4 shows data from the quantum computer IBM-Q Lagos. See Appendix D for details about the quantum computer. Figure 4a shows the free energy as a function of temperature. Figure 4b shows the difference in free energy between the data from the simulation and the data from the quantum device. The error from the quantum device is less than \(10\%\) at \(T=0\) and decreases as the temperature is raised. This is due to the fact that at higher temperature, the density operator becomes more evenly spread across the eigenspectrum, so the free energy is tolerant of inexact quantum operators.

5 Discussion

While we focus on calculation of the Helmholtz free energy using a variational approach, we foresee the use of mid-circuit measurements to generate entropy as a component that can be used in a variety of algorithms. Mid-circuit measurements can be used, in general, to both increase and decrease the entropy as a subroutine in a quantum algorithm.

Above, we go into detail about how mid-circuit measurements can be used to increase entropy. More care is required in order to decrease entropy. As an expression of the second law of thermodynamics, we cannot reduce the entropy of the quantum system by measurement alone. If we want to reduce the entropy, we must perform postselection using the measurement results. By sorting out shots with certain measurement outcomes, we artificially set some of the \(P_m = 0\) in Eq. (24). This is analogous to the idea of a Maxwell Demon, which lowers the entropy of a system by selectively allowing only low energy particles through a screen. Postselection, like the Maxwell Demon, does not lower the entropy of the universe but simply redefines what we consider to be the system.

6 Conclusion

We present a method for generating quantum ensembles on quantum computers using mid-circuit measurements. The ability to generate mixed-state quantum ensembles is a critical component in simulating any physical system that is not isolated from its environment. Our work establishes a relationship between entropy and quantum measurements, which applies to all quantum algorithms that utilize mid-circuit measurements. We use this relationship to construct an algorithm for finding the free energy of open systems on a quantum computer. We use the measurement apparatus as the environment for the open system. This allows us to avoid using auxiliary qubits. Furthermore, as entropy is unchanged after the mid-circuit measurement, we do not need to measure the final entropy, which can be challenging. Thus, we have two main advantages. First, because we do not use auxiliary qubits, the amount of quantum resources needed for our algorithm is greatly reduced. Second, the entropy does not need to be approximated because it is provided as an input parameter. Furthermore, our method is a natural extension of the variational quantum eigensolver and is practical for near-term quantum hardware.