Keywords

figure a
figure b

1 Introduction

Classical simulation of quantum computing [3, 17, 70, 77] serves as a crucial task in the verification [6, 37, 56, 68], synthesis [2, 13, 62] and optimization [15, 63] of quantum circuits. In addition, improved classical simulation methods aid the search for a quantum supremacy [8, 39]. Due to the inherent exponential size of the underlying representations of quantum states and operations, classical simulation of quantum circuits is a highly non-trivial task that comes in two flavors [55]: Weak simulators only sample the probability distribution over measurement outcomes, i.e, they implement the “bounded-error” \(\textsf{BQP}\)-complete problem that a quantum computer solves, whereas strong circuit simulators can compute the amplitude of any basis state, solving a #\(\textsf{P}\)-complete problem [28, 40].

Like SAT solvers [12, 30], (weighted) model counting (WMC) tools have shown great potential for solving problem instances arising from industrial applications [54, 60], despite the #P-completeness of the problem of counting the number (or weights) of satisfying assignments. Because strong quantum circuit simulation is #\(\textsf{P}\)-complete, WMC is a natural fit that nonetheless has not yet been exploited. To do so, we provide the first encoding of the strong simulation problem of universal quantum circuits as a WMC problem. Perhaps surprisingly, this encoding is linear in the number of gates m or qubits n, i.e., \(\mathcal O(n+m)\).

One of the key properties of our encoding is that it does not use complex numbers to encode the probability amplitudes of quantum states, which would prohibit the use of all modern model counters. We achieve this by exploiting a generalization [76] of the Gottesman-Knill theorem [35], which in effect rewrites the density matrix of any quantum state as a linear combination of stabilizer states [33] with real coefficients. It turns out that many exact WMC tools do support negative weights out of the box. Our encoding thus empowers them to reason directly about the constructive and destructive interference ubiquitous in quantum algorithms.

Like many, our method builds on the Solovay-Kitaev theorem [27] that in particular shows that the Clifford+T gate set is universal for quantum computing, meaning that this gate set can efficiently approximate any unitary operator. Since our encoding also supports arbitrary rotation gates (phase shift P, \(R_X\), \(R_Y\) and \(R_Z\)), and since these rotations are non-Clifford gates, it can also support other universal gate sets like Clifford+\(R_X\) or Clifford+P, which allows for easier approximations. One important example is Quantum Fourier Transformation (QFT), which can be simulated with O(n) rotation gates while needs at least \(O(n\log (n))\) T gates to approximate [51]. Moreover, since the hardness of exact reasoning about quantum circuits depends on the gate set (the classes \(\textsf{EQP}\) and \(\textsf{NQP}\) are parameterized by the gate set as it defines the realizable unitaries), this flexibility significantly enhances the power of our strong simulation approach.

We implement our encoding in an open-source tool QCMC. We demonstrate the scalability and feasibility of the proposed encoding through experimental evaluations based on three classes of benchmarks: random Clifford+T circuits mimicking quantum chemistry applications [75] and oracles, and quantum algorithms from MQT bench [58]. We compare the results of our method against state-of-the-art circuit simulation tools QuiZX [41] and Quasimodo [64], respectively based on the ZX-calculus [25] and CFLOBDD [65]. QCMC simulates important quantum algorithms like QAOA, W-state, VQE, and others which can not be directly supported by QuiZX. Additionally, QCMC outperforms Quasimodo on almost all random circuits and uses orders of magnitudes less memory than Quasimodo on all benchmarks.

In sum, this paper makes the following contributions.

  • a generalized stabilizer formalism defined in terms of stabilizer groups, which form a basis for our encoding;

  • the first encoding for circuits in various universal quantum gate sets as a weighted model counting problem, which is also linear in size;

  • an implementation based on the weighted model counting tool GPMC;

  • new benchmarks for the WMC competition [4], and insights on improving model counters and samplers for applications in quantum computing.

2 Preliminaries

2.1 Quantum Computing

Similar to bits in classical computing, a quantum computer operates on quantum bits, or short as qubits. A bit is either 0 or 1, while a qubit can be in states \(|0\rangle \), \(|1\rangle \) or a superposition of both. Here ‘\(| \rangle \)’ is the Dirac notation, representing a unit column vector, i.e., \(|0\rangle = [1,0]^\textrm{T}\) and \(|1\rangle = [0,1]^\textrm{T}\), while \(\langle \psi |\) denotes the complex conjugate and transpose of \(|\psi \rangle \), that is a row vector: \(\langle \psi | = |\psi \rangle ^\dag \).

In this paper, we fix n to be the number of the qubits. Let \(\mathcal {H}\) be a Hilbert space. An n-qubit quantum state is a \(2^n\)-dimension unit column vector in \(\mathcal {H}\). In the case of \(n = 1\), a pure state \(|\psi \rangle \) is written as \(|\psi \rangle = \alpha |0\rangle + \beta |1\rangle \), where \(\alpha \) and \(\beta \) are complex numbers in \(\mathbb {C} \) satisfying \(|\alpha |^2 + |\beta |^2 = 1\). To extend from single-qubit states to multiple-qubit states we use the tensor or Kronecker product, which is defined as follows: given \(r_A \times c_A\) matrix A and \(r_B \times c_B\) matrix B, the \(r_A r_B \times c_A c_B\) matrix \(A \otimes B\) is

$$ A \otimes B = \begin{bmatrix} A_{00} B &{} A_{01}B &{}\ldots &{} A_{0 c_A} B\\ A_{10} B &{} A_{11}B &{}\ldots &{} A_{1 c_A} B\\ \vdots &{}\vdots &{}\ddots &{}\vdots \\ A_{r_A 0} B &{} A_{r_A 1}B &{}\ldots &{} A_{r_A c_A} B\\ \end{bmatrix} . $$

A computational basis state \(|\vec x\rangle \) for \(\vec x\in \left\{ 0,1 \right\} ^{n}\) is a vector with all entries set to 0 except at index \(\vec x\), which is 1. For two computational basis states \(|\vec x\rangle , |\vec y\rangle \), we have that \(|\vec x\rangle \otimes |\vec y\rangle = |\vec x \vec y\rangle \). For example, a two-qubit state \(|0\rangle \otimes |0\rangle \) equals \(|00\rangle = [1,0,0,0]^\textrm{T}\). Sometimes we represent a pure quantum state \(|\psi \rangle \) by the density operator obtained as the product \(|\psi \rangle \langle \psi |\) of the state with itself. Density operators U are Hermitian matrices, i.e., they satisfy \(U^\dagger = U\).

Operations on quantum states are given by quantum gates. For an n-qubit quantum system, a (global) quantum gate is a function \(\textbf{G}: \mathcal {H}\rightarrow \mathcal {H}\), which can be described by \(2^n\times 2^n\) unitary matrix U, i.e., with the property that \(UU^\dag = U^\dag U = I\). A quantum gate is local when it works on a subspace of a quantum system, which can be extended to a global quantum gate by applying identity operators on unchanged qubits, i.e. a local quantum gate U on qubit j can be represented as a global quantum gate \(U_j = I^{\otimes j} \otimes U \otimes I^{\otimes n - j -1}\). Examples of quantum gates are the \(2 \times 2\) Pauli matrices (or Pauli gates) X, Y, Z, and the identity matrix I:

figure c

Together with the identity matrix I, the Pauli matrices form a basis for \(2 \times 2\) Hermitian matrix space. Let \(\textrm{PAULI}_n\) be the set of the tensor product of n Pauli operators (a “Pauli string”). The so-called Pauli group is generated by multiplication of the local Pauli operators XZ as follows: \(\mathcal {P}_n = \left\langle X_0, Z_0 \ldots , X_{n-1}, Z_{n-1} \right\rangle \). Structurally, the Pauli group can be written as \(\mathcal {P}_n = \left\{ \lambda P \mid P\in \textrm{PAULI}_n , \lambda \in \left\{ \pm 1, \pm \dot{\imath } \right\} \right\} \). For instance, we have \(-X\otimes Y \otimes Z \in \mathcal {P}_3\). The Pauli strings form a basis for the full Hermitian matrix space [48, 52].

Let [m] be \(\{0,\ldots , m-1\}\). The evolution of a quantum system can be modeled by a quantum circuit, which is a sequence of quantum gates \(C\equiv (\textbf{G}^0, \ldots , \textbf{G}^{m-1})\), Here \(\textbf{G}^t\) represents a global quantum gate at time step \(t\in [m]\). Let \(U^t\) be the unitary matrix for \(\textbf{G} ^t\). Then C can be expressed by the unitary matrix \(U = U^{m-1}\cdots U^0\).

An important class of quantum circuits is the so-called Clifford group, as it can describe interesting quantum mechanical phenomena such as entanglement, teleportation, and superdense encoding. More importantly, they are widely used in quantum error-correcting codes [18, 66] and measurement-based quantum computation [59]. The Clifford group is a set of unitary operators that map the Pauli group to itself through conjugation, i.e. all the \(2^n \times 2^n\) unitary matrices U such that \(UPU^\dag \in \mathcal {P}_n \) for all \(P\in \mathcal {P}_n \). It is generated by the local Hadamard (H) and phase (S) gate, and the two-qubit control-not gate (CX, CNOT):

figure d

Recall that \(U_j\) performs U on j-th qubit. Similarly, we denote by \(CX_{ij}\) the unitary operator taking the i-th qubit as the control qubit and j-qubit as the target to execute a controlled-not gate. Clifford circuits are circuits only containing gates from the Clifford group.

A (projective) measurement is given by a set of projectors \(\{\mathbb {P}_0,\ldots ,\mathbb {P}_{k-1}\}\) —one for each measurement outcome [k]— satisfying \(\sum _{j\in [k]}\mathbb {P}_j = I\). A linear operator \(\mathbb {P}\) is a projector if and only if \(\mathbb {P}\mathbb {P}=\mathbb {P}\). For example, given a three-qubit system, measuring the first two qubits under computational basis is given by the measurement \(\left\{ ~~ |0\rangle \langle 0|\otimes |0\rangle \langle 0|\otimes I,~~~ |1\rangle \langle 1|\otimes |0\rangle \langle 0|\otimes I,~~~ |0\rangle \langle 0|\otimes |1\rangle \langle 1|\otimes I,~~~ |1\rangle \langle 1|\otimes |1\rangle \langle 1|\otimes I ~~ \right\} \).

Weak simulation is the problem of sampling the measurement outcomes according to the probability distribution induced by the semantics of the circuit. In this work, we focus on strong simulation as defined in Definition 1. Here we assume, without loss of generality, that a circuit is initialized to the all-zero state: \(|0\rangle ^{\otimes n}\).

Definition 1

(Strong simulation [21]). Given an n-qubit quantum circuit C and a measurement \(M=\{\mathbb {P}_0,\ldots ,\mathbb {P}_{k-1}\}\), a strong simulation of circuit C computes the probability of getting any outcome \(l\in [k]\), that is, the value \(\langle 0|^{\otimes n}C^\dag \mathbb {P}_l C|0\rangle ^{\otimes n}\), up to a number of desired bits of precision.

The Gottesman-Knill theorem [35] shows that Clifford circuits can be strongly stimulated by classical algorithms in polynomial time and space.

2.2 Stabilizer Groups

The stabilizer formalism [35] is a subset of quantum computing that can be effectively simulated on a classical computer. A state \(|\varphi \rangle \) is said to be stabilized by a quantum unitary operator U if and only if it is a +1 eigenvector of U, i.e., \(U|\varphi \rangle = |\varphi \rangle \). For example, we say \(|0\rangle \) is stabilized by Z as \(Z|0\rangle = |0\rangle \). Similarly, \(|+\rangle = \frac{1}{\sqrt{2}}|0\rangle + \frac{1}{\sqrt{2}}|1\rangle \) is stabilized by X, and all states are stabilized by I. The stabilizer states form a strict subset of all quantum states which can be uniquely described by maximal commutative subgroups of the Pauli group \(\mathcal {P}_n \), which is called stabilizer group. The elements of the stabilizer group are called stabilizers. Recall that the Clifford group is formed by unitary operators mapping the Pauli group to itself. This leads to the fact that stabilizer states are closed under operators from the Clifford group.

Given an n-qubit stabilizer state \(|\psi \rangle \), let \(\mathcal {S}_{|\psi \rangle }\) be the stabilizer group of \(|\psi \rangle \). While the elements of a Pauli group \(\mathcal {P}_n \) either commute or anticommute, a stabilizer group \(\mathcal {S}\) must be abelian, because if \(P_1, P_2\in \mathcal {S}_{|\varphi \rangle }\) anticommute, i.e, \(P_1P_2 = -P_2P_1\), there would be a contradiction: \(|\varphi \rangle = P_1P_2|\varphi \rangle = -P_2P_1|\varphi \rangle = - |\varphi \rangle \). In particular, \(-I^{\otimes n}\) can never be a stabilizer. In fact, a subgroup \(\mathcal {S}\) of \(\mathcal {P}_n \) is a stabilizer group for an n-qubit quantum state if and only if it is an abelian group without \(-I^{\otimes n}\). Therefore, for any Pauli string \(P\in \textrm{PAULI}_n\), if \(\lambda P\in \mathcal {S}\), then it holds that \(\lambda =\pm 1\), since for all \(\lambda P\in \mathcal {S}\), we have \( (\lambda P)|\varphi \rangle = (\lambda P)^2|\varphi \rangle = \lambda ^2 I |\varphi \rangle = \lambda ^2|\varphi \rangle \Rightarrow \lambda = \pm 1.\)

Any stabilizer group \(\mathcal {S}\) can be specified by a set of generators \(\mathcal {G}\) so that every element in \(\mathcal {S}\) can be obtained through matrix multiplication on \(\mathcal {G}\), denoted as \(\left\langle \mathcal {G} \right\rangle = \mathcal {S}\). The set of generators \(\mathcal {G}\) needs not to be unique and has order \(|\mathcal {G}| = n\), where n represents the number of qubits, and the corresponding stabilizer group \(\mathcal {S}\) has order \(2^{|\mathcal {G}|}\).

Example 1

The Bell state \(|\varPhi _{00}\rangle = \frac{1}{\sqrt{2}}(|00\rangle + |11\rangle )\) can be represented by the following stabilizer generators written in square form:

figure e

   \(\triangle \)

We can relate the (generators of the) stabilizer group directly to the stabilizer state \(|\psi \rangle \), as the density operator of the stabilizer state can be written in a linear combination of Pauli matrices as follows [69].

$$\begin{aligned} |\psi \rangle \langle \psi | = \prod _{G\in \mathcal {G}_{|\psi \rangle }} \frac{I+G}{2} = \frac{1}{2^n}\sum _{S\in \mathcal {S}_{|\psi \rangle }} S. \end{aligned}$$
(1)

where \(S=\pm P\) for \(P\in \textrm{PAULI}_n\) in Clifford circuits. If a Clifford gate U is applied to the above state, i.e. \(U|\psi \rangle \), and let \(S \in \mathcal {S}_{|\psi \rangle }\), the corresponding stabilizers of \(U|\psi \rangle \) can be obtained by \(USU^\dag \) since \(USU^\dag U|\psi \rangle = U|\psi \rangle \). Thus we have \(\mathcal {S}_{U|\psi \rangle } = \{USU^\dag \ | \ S\in \mathcal {S}_{|\psi \rangle }\}\) and the density operator of the resulting state will be obtained by conjugating U on \(|\psi \rangle \langle \psi |\), i.e. \(U|\psi \rangle \langle \psi |U^\dag = \frac{1}{2^n}\sum _{S\in \mathcal {S}_{|\psi \rangle }} USU^\dag \). To be specific, consider performing a Clifford gate \(U_j\), denoting a single qubit gate U applied to j-th qubit as given in previous section, to a stabilizer \(S = \pm P_0\otimes \ldots \otimes P_{n-1}\). We have \(U_jSU_j^\dag = \pm P_0 \otimes \ldots \otimes UP_jU^\dag \otimes \ldots \otimes P_{n-1}\). Since \(U_j\) is a Clifford gate, \(U_jSU_j^\dag \in \mathcal {P}_n \). Thus the j-th entry needs to be updated, which can be done in constant time following the rules in Table 1 for H and S. Applying a two-qubit gate \(CX_{ij}\) is similar to updating the sign and Pauli matrices in i-th and j-th position (see Table 1), which also takes constant time. Overall, updating all generators after performing one Clifford gate can be done in O(n) time.

Example 2

The Bell state \(|\varPhi _{00}\rangle = \frac{1}{\sqrt{2}}(|00\rangle + |11\rangle )\) is a stabilizer state, as it can be obtained by the following circuit, which evaluates to \(CX_{01} \cdot H_0 \cdot |00\rangle = |\varPhi _{00}\rangle \):

figure f

We can simulate the above circuit with the stabilizer formalism. The stabilizer generator set for each time step can be obtained using the rules shown in Table 1:

$$ \begin{aligned} \left\langle \begin{matrix} Z\otimes I \\ I \otimes Z \end{matrix} \right\rangle &\xrightarrow { H_0} \left\langle \begin{matrix} HZH^\dag \otimes I \\ HIH^\dag \otimes Z \end{matrix} \right\rangle = \left\langle \begin{matrix} X \otimes I \\ I \otimes Z \end{matrix} \right\rangle \xrightarrow { CX_{0,1}} \left\langle \begin{matrix} CX(X \otimes I)CX^\dag \\ CX (I \otimes Z)CX^\dag \end{matrix} \right\rangle = \left\langle \begin{matrix} X \otimes X \\ Z \otimes Z \end{matrix} \right\rangle . ~~~~ \end{aligned} $$

   \(\triangle \)

Table 1. Lookup table for the action of conjugating Pauli gates by Clifford gates. The subscripts “c” and “t” stand for “control” and “target”. Adapted from [32].

The above explains the essence of the Gottesmann-Knill theorem [35], which states that Clifford circuits can be classically simulated by describing n-qubit stabilizer states by their stabilizer generator set instead of a \(2^n\) complex vector. It is important to note that Clifford gates do not constitute a universal set of quantum gates. However, by adding any non-Clifford gate, such as the \(T = |0\rangle \langle 0| + e^{\dot{\imath }\pi /4}|1\rangle \langle 1|\) gate, it becomes possible to approximate any unitary operator with arbitrary accuracy, as shown in [14, 42]. Moreover, the space of n-qubit density matrices has a basis in the n-qubit stabilizer states [33], which allows us in Sect. 3 to extend the stabilizer formalism to a general quantum state.

2.3 Weighted Model Counting

Weighted model counters can solve probabilistic reasoning problems with real-valued probabilities like Bayesian inference [20]. By reinterpreting quantum states in the stabilizer-state basis, we obviate the need for complex amplitudes typical in quantum computing, as shown in the next section. It turns out that existing weighted model counting tools, like GPMC [36], already support negative weights (see Sect. 4).

Let \(\mathbb {B} \) be the Boolean set \(\left\{ 0,1 \right\} \). A propositional formula \(F :\mathbb {B} ^V \rightarrow \mathbb {B} \) over a finite set of Boolean variables V is satisfiable if there is an assignment \(\alpha \in \mathbb {B} ^V\) such that \(F(\alpha ) = 1 \). We define the set of all satisfiable assignments of a propositional formula F as \(SAT(F):= \left\{ \alpha \mid F(\alpha ) = 1 \right\} \). We write an assignment \(\alpha \) as a cube (a conjunction of literals, i.e. positive or negative variables), e.g., \(a\wedge b\), or shorter ab.

A weight function \(W:\left\{ \overline{v}, v \mid v\in V \right\} \rightarrow \mathbb {R} \) assigns a real-valued weight to positive literals v (i.e., \(v = 1 \)) and the negative literals \(\overline{v}\) (i.e., \(v = 0 \)). We say a variable v is unbiased iff \(W(v) = W(\overline{v}) = 1\). Given an assignment \(\alpha \in \mathbb {B} ^V\), let \(W(\alpha (v)) = W(v = \alpha (v))\) for \(v\in V\). For a propositional formula F over variables in V and weight function W, we define weighted model counting as follows.

$$ MC_W(F) \,\triangleq \,\sum _{\alpha \in \mathbb {B} ^V} F(\alpha )\cdot W(\alpha )\text {, where } W(\alpha ) = \prod _{v\in V} W(\alpha (v)). $$

Example 3

Given the formula \(F = v_1 v_2 \vee \overline{v_1} v_2 \vee v_3\) over \(V=\{v_1,v_2, v_3\}\), there are two satisfying assignments: \(\alpha _1 = v_1 v_2 v_3\) and \(\alpha _2 = \overline{v_1} v_2 v_3\). We define the weight function W as \(W(v_1) = -\frac{1}{2}\), \(W(\overline{v_1}) = \frac{1}{3}\) and \(W(v_2) = \frac{1}{4}\), \(W(\overline{v_2}) = \frac{3}{4}\), while \(v_3\) remains unbiased. The weight of F can be computed as \(MC_W(F) = -\frac{1}{2} \times \frac{1}{4} \times 1 + \frac{1}{3} \times \frac{1}{4} \times 1 = -\frac{1}{24}\).    \(\triangle \)

3 Encoding Quantum Circuits as Weighted CNF

3.1 Generalized Stabilizer Formalism

Clifford circuits together with T gates generate states beyond stabilizer states, enabling universal quantum computation. As is shown in Table 1, Clifford gates map the set of Pauli matrices to itself, keeping stabilizers within the Pauli group. In contrast, T gates can transform a Pauli matrix into a linear combination of Pauli matrices. To be specific, Table 2 gives the action of T gates on different Pauli gates. Given a Pauli string, after performing Clifford+T gates, we will get a summation of weighted Pauli strings, e.g., \(T_0 \cdot (X\otimes X) \cdot T_0^\dag = \frac{1}{\sqrt{2}}(X\otimes X+Y \otimes X)\) where \(T_0 = T\otimes I\) as defined in Sect. 2. This leads to the definition of generalized stabilizer state extended from standard stabilizer formalism.

Definition 2

In an n-qubit quantum system, a generalized stabilizer state \(|\psi \rangle \) is the simultaneous eigenvector, with eigenvalue 1, of a group containing \(2^n\) commuting unitary operators S. The set of S is a generalized stabilizer group.

The above definition is adapted from [76] by defining a generalized stabilizer state using a generalized stabilizer group instead of generalized stabilizer generators. In this way, we can easily get the corresponding stabilizer state by a weighted summation of its stabilizers to avoid multiplications between Pauli strings (the middle part of Eq. 1). The following proposition is also adapted from [76], where they demonstrate that any pure state can be uniquely described by a set of stabilizers. We additionally show that there exists a set of stabilizers, forming a group, which uniquely describes any pure state.

Proposition 1

  For any pure state \(|\varphi \rangle \) in an n-qubit quantum system, there exists a generalized stabilizer group \(\mathcal {S}_{|\varphi \rangle }\) such that \(|\varphi \rangle \langle \varphi | = \frac{1}{2^n} \cdot \sum _{S\in \mathcal {S}_{|\varphi \rangle }} S\).

Proof

Given a pure state \(|\varphi \rangle \), we have \(|\varphi \rangle = U|0\rangle ^{\otimes n}\), where U is a unitary operator. Let \(\mathcal {S}_{|\varphi \rangle } = \{ USU^\dag \mid S \in \mathcal {S}_{|0\rangle ^{\otimes n}} \}\), which is an isomorphic group to \(\mathcal {S}_{|0\rangle ^{\otimes n}}\) since U is unitary. For any \(S\in \mathcal {S}_{|\varphi \rangle }\), we have \(S'\in \mathcal {S}_{|0\rangle ^{\otimes n}}\) satisfying \(S = US'U^\dag \) and \(S|\varphi \rangle = US'U^\dag U|0\rangle ^{\otimes n} = US'|0\rangle ^{\otimes n} = U|0\rangle ^{\otimes n} = |\varphi \rangle \). Hence \(S_{|\varphi \rangle }\) is the generalized stabilizer group of state \(|\varphi \rangle \). Furthermore, we have \(|\varphi \rangle \langle \varphi | = U|0\rangle \langle 0|U^\dag = U(\frac{1}{2^n}\sum _{S'\in \mathcal {S}_{|0\rangle ^{\otimes n}}} S')U^\dag = \frac{1}{2^n}\sum _{S'\in \mathcal {S}_{|0\rangle ^{\otimes n}}} U S' U^\dag = \frac{1}{2^n} \cdot \sum _{S\in \mathcal {S}_{|\varphi \rangle }} S\). In fact, given that any generalized stabilizer \(S \in \mathcal {S}_{|\varphi \rangle }\) is a Hermitian matrix, since \(S = US'U^\dag = US'^\dag U^\dag = S^\dag \) where \(S'\in \mathcal {S}_{|0\rangle ^{\otimes n}}\), according to [48, Lemma 1], it can be expressed as \(S = \sum _{P\in \textrm{PAULI}_n} \lambda _P P\), where \(\lambda _P \in \mathbb {R}\). Thus a generalized stabilizer is always a linear combination of stabilizers.    \(\square \)

Table 2. Lookup table for the action of conjugating Pauli gates by T gates.

For an n-qubit quantum space, let \(GStab \) be the set of generalized stabilizer states, which is also the set of all pure states. Let \(\mathcal {Q}\) be the set of quantum states generated from a Clifford+T circuit starting from the all-zero state, i.e. \(\mathcal {Q} = \{U_{m-1} \ldots U_0|0\rangle ^{\otimes n} \mid U_i\in \{H,S,CX,T\}\}\). We have \(Stab \subset \mathcal {Q} \subset GStab \) and any pure state in \(GStab \) can be approximated by some state in \(\mathcal {Q}\) with arbitrary accuracy [27]. For any \(|\varphi \rangle \in Stab \), we have \(\mathcal {S}_{|\varphi \rangle } = \{ \pm P \mid P\in \textrm{PAULI}_n\}\). For any \(|\psi \rangle \in GStab \), we have \(\mathcal {S}_{|\psi \rangle } = \left\{ \sum \lambda _P P \mid P \in \textrm{PAULI}_n, \lambda _P \in \mathbb {R} \right\} \). E.g., when applying Clifford+T gates on \(|\varphi \rangle \), the stabilizer group \(\mathcal {S}_{|\psi \rangle }\) has \(\lambda _P\) equal to 0 or \(\frac{1}{\sqrt{2}^k}\) for \(k \in \mathbb {N}^+\) (as derived by updating \(\mathcal {S}_{|\varphi \rangle }\) based on Table 1 and Table 2). Combining this with Proposition 1, we may flatten summations of generalized stabilizers:

$$\begin{aligned} |\psi \rangle \langle \psi | = \frac{1}{2^n}\sum _{S\in \mathcal {S}_{|\psi \rangle }} \sum _{\lambda _P P \in S} \lambda _P P = \frac{1}{2^n} \sum _{P \in \textrm{PAULI}_n} \lambda _P P, \end{aligned}$$
(2)

Hence the density operator is determined by a summation of weighted Pauli strings and there is no need to distinguish which of the \(2^n\) generalized stabilizers a Pauli string belongs to. (Stated differently, the density matrix can be decomposed in the Pauli basis [48].) We exploit this fact in our encoding and Example 4.

Example 4

Reconsider the circuit in Example 2 and add T, CX and H gates:

figure g

Continuing from Example 2, we obtain the following generalized generators, where \(X' = \tfrac{1}{\sqrt{2}}(X+Y)\) and \(Z' = \tfrac{1}{\sqrt{2}}(Z-Y)\) as in Table 2.

$$ \begin{aligned} \underbrace{ \left\langle \begin{matrix} X\otimes X \\ Z \otimes Z \end{matrix} \right\rangle }_{|\varphi _2\rangle } &\xrightarrow {T_0} \underbrace{ \left\langle \begin{matrix} X' \otimes X \\ Z \otimes Z \end{matrix} \right\rangle }_{|\varphi _3\rangle } \xrightarrow {CX_{0,1}} \underbrace{ \left\langle \begin{matrix} X' \otimes I \\ I \otimes Z \end{matrix} \right\rangle }_{|\varphi _4\rangle } \xrightarrow {H_0} \underbrace{ \left\langle \begin{matrix} Z'\otimes I \\ I \otimes Z \end{matrix} \right\rangle }_{|\varphi _5\rangle } \end{aligned}. $$

In our encoding, as in the above definition of generalized stabilizer states, we let satisfying assignments represent not just the generator set, but the entire stabilizer groups. These groups are: \( \mathcal {S}_{|\varphi _2\rangle } = \{X \otimes X,Z \otimes Z,-Y \otimes Y,I \otimes I\}\), \(\mathcal {S}_{|\varphi _3\rangle } = \{X' \otimes X, Z \otimes Z, -Y' \otimes Y,I \otimes I\}\), \(\mathcal {S}_{|\varphi _4\rangle } = \{X'\otimes I,I \otimes Z, X'\otimes Z,I \otimes I\}\) and \(\mathcal {S}_{|\varphi _5\rangle } = \{Z' \otimes I,I \otimes Z, Z'\otimes Z,I \otimes I\}\), where \(Y' = \frac{1}{\sqrt{2}}(Y-X)\).

Finally, according to Eq. 2, we may equally expand e.g. \(\mathcal {S}_{|\varphi _5\rangle }\) to:

\(\mathcal {S}_{|\varphi _5\rangle }' ~~=~~ \{ \frac{1}{\sqrt{2}} Z \otimes I,~~ -\frac{1}{\sqrt{2}} Y \otimes I,~~I \otimes Z,~~ \frac{1}{\sqrt{2}} Z \otimes Z,~~ -\frac{1}{\sqrt{2}} Y \otimes Z,~~ I \otimes I\}\).    \(\triangle \)

3.2 Encoding Clifford+T Circuits

Since generalized stabilizer states can be determined by a sum of weighted Pauli strings as shown in Eq. 2, we encode a state by Boolean constraints whose satisfying assignments represent those weighted Pauli strings. The idea is to encode the sign, the Pauli string, and the weights separately. We will start with encoding the Pauli string and the sign.

Table 3. Boolean variables under the action of conjugating one T gate. Here we omit the sign \((-1)^{r^{t}}\) for all operators G and sign \((-1)^{r^{t+1}}\) for all \({TGT^\dag }\).

A Pauli string \(P\in \textrm{PAULI}_n\) can be encoded by 2n Boolean variables as \(\sigma [x_{0},z_{0}] \otimes \ldots \otimes \sigma [x_{n-1},z_{n-1}]\), where the j-th Pauli matrix is indicated by variables \(x_j\) and \(z_j\). To encode the sign, only one Boolean variable r is needed. We introduce weighted model counting to interpret the sign by defining \(W(r) = -1\) and \(W(\overline{r}) = 1\). Thus \(\pm P\in \pm \textrm{PAULI}_n\) can be interpreted as \((-1)^r\otimes _{i\in [n]}\sigma [x_i,z_i]\). For example, consider the Boolean formula \(r\lnot x_0 z_0 x_1 z_1\). Its only satisfying assignment is \(\{r\rightarrow 1, x_0 \rightarrow 0, z_0\rightarrow 1, x_1 \rightarrow 1, z_1\rightarrow 1\} \equiv -Z\otimes Y\). Without loss of generality, we set the initial state to be all zero state \(|0\ldots 0\rangle \), whose stabilizer group is \(\mathcal {S}_{|0\ldots 0\rangle } = \{\otimes _{i\in [n]} P_i \mid P_i\in \{Z,I\}\} \equiv \{(-1)^r\otimes _{i\in [n]}\sigma [x_i,z_i] \mid x_i = 0, z_i\in \{0,1\} and r = 0\}\). Hence the Boolean formula for the initial state is defined as \(F_{init}(\boldsymbol{x}^0,\boldsymbol{z}^0,r^0) \,\triangleq \,\lnot r^0 \wedge \bigwedge _{j\in [n]}\lnot x^0_{j}\), where we use superscripts, e.g., \(r^t, x_0^t, z_0^t\), to denote variables at time step t (after t gates from the circuit have been applied). Note that we assign a weight to r only on the final time step, as explained later.

Example 5

Consider the initial state \(|00\rangle \) in Example 4. The corresponding constraint at time step 0 is \(\lnot r^0\lnot x^0_{0}\lnot x^0_{1}\), which has satisfying assignments:

$$ \left\{ \begin{aligned} &\{r^0 \rightarrow 0, x_0^0 \rightarrow 0, x_1^0\rightarrow 0, z_0^0\rightarrow 1, z_1^0\rightarrow 1\} \\ &\{r^0 \rightarrow 0, x_0^0 \rightarrow 0, x_1^0\rightarrow 0, z_0^0\rightarrow 1, z_1^0\rightarrow 0\} \\ &\{r^0 \rightarrow 0, x_0^0 \rightarrow 0, x_1^0\rightarrow 0, z_0^0\rightarrow 0, z_1^0\rightarrow 1\} \\ &\{r^0 \rightarrow 0, x_0^0 \rightarrow 0, x_1^0\rightarrow 0, z_0^0\rightarrow 0, z_1^0\rightarrow 0\} \end{aligned} \right\} \equiv \left\{ \begin{aligned} Z \otimes Z\\ Z \otimes I\\ I \otimes Z\\ I \otimes I \end{aligned} \right\} $$

   \(\triangle \)

While we encode those signed Pauli strings using variables from \(\left\{ x_{j}, z_{j}, r \mid j\in [n] \right\} \), to encode the weights, we introduce new variables u. When a \(T_j\) is performed and \(x_{j}=1\), which means executing a T gate on j-th qubit with certain stabilizer being either \(\pm X\) or \(\pm Y\), we set \(u = 1\) to indicate a branch of the operator, i.e. \(TXT^\dag = X' = \frac{1}{\sqrt{2}}(X+Y)\) and \(TYT^\dag = Y' =\frac{1}{\sqrt{2}}(Y-X)\). Therefore, for each generalized stabilizer state in a circuit with m gates and n qubits, the encoding uses the set of variables \(V^t = \{x^t_{j}, z^t_{j}, r^t, u_{t}\mid \ j\in [n]\}\), where \(t\in \{0,\ldots ,m\}\) denotes a time step. Table 3 illustrates the details of how the Boolean variables in \(V^t\) change over a T gate. Here each satisfying assignment indicates a weighted Pauli string, for example, there are two assignments for a stabilizer \(\frac{1}{\sqrt{2}}(X+Y)\).

Using Table 1 and Table 3, given a single-qubit Clifford+T gate \(\textbf{G} _{j}\) on qubit j at time step t (or \(CX_{j,k}\) on qubits jk), we can derive a Boolean formula \(F_{\textbf{G} _j}(V^t,V^{t+1})\), abbreviated as \(\textbf{G} _j^t\), as in the following.

$$\begin{aligned} \begin{aligned} H^t_j \,\triangleq \,~&r^{t+1} \Longleftrightarrow r^{t} \oplus x^t_{j}z^t_{j} ~~\wedge ~~ z^{t+1}_{j} \Longleftrightarrow x^t_{j} ~~\wedge ~~ x^{t+1}_{j} \Longleftrightarrow z^t_{j}\\ S^t_j \,\triangleq \,~&r^{t+1} \Longleftrightarrow r^{t} \oplus x^t_{j}z^t_{j} ~~\wedge ~~ z^{t+1}_{j} \Longleftrightarrow x^t_{j} \oplus z^t_{j} \\ CX^t_{j,k} \,\triangleq \,~& r^{t+1} \Longleftrightarrow r^{t} \oplus x^t_{j}z^t_{k} (x^t_{k} \oplus \lnot z^t_{j}) \ ~~\wedge ~~ x^{t+1}_{k} \Longleftrightarrow x^t_{k} \oplus x^t_{j} ~~\wedge ~~ \\ & z^{t+1}_{j} \Longleftrightarrow z^t_{j} \oplus z^t_{k} \\ T^t_j \,\triangleq \,~&x^{t+1}_{j} \Longleftrightarrow x^t_{j} \quad \wedge \quad x^{t}_{j} \vee (z^{t+1}_{j} \Leftrightarrow z^t_{j}) \quad \wedge \\ & r_i^{t+1} \Longleftrightarrow r^t \oplus x^t_{j} z^t_{j} \lnot z^{t+1}_{j} \quad \wedge \quad u_{t} \Longleftrightarrow x^t_{j}. \end{aligned} \end{aligned}$$
(3)

The above omits additional constraints \(v^{t+1} \Leftrightarrow v^t\) for all unconstrained time-step-\(t+1\) variables, i.e., for all \(v^t \in V_l^t\) with \(l \ne j, k\). In fact, it is not necessary to allocate new variables for those unconstrained time-step-\(t+1\) variables. The constraint \(v^{t+1} \Leftrightarrow v^t\) can be effectively implemented by reusing the Boolean variables \(v^t\) for \(v^{t+1}\). Thus for each time step, only a constant number of new variables need to be allocated. For instance, when applying \(H_j\) gate, we only need one new variable \(r^{t+1}\), since we can reuse the variable of \(x_j^t\) for \(z_j^{t+1}\) and \(z_j^t\) for \(x_j^{t+1}\). And when applying \(CX_{ij}\) gate, we need three new variables for \(r^{t+1}\), \(x_j^{t+1}\) and \(z_j^{t+1}\). Additionally, since variables for all \(x_j^0\) and \(z_j^0\) with \(j\in [n]\) are allocated initially, and as shown below, performing a measurement introduces no new variable, the size of our encoding is \(O(n+m)\).

To this end, given a Clifford+T circuit \(C=(\textbf{G} ^0,\ldots , \textbf{G} ^{m-1})\) without measurements, we can build the following Boolean constraint.

$$\begin{aligned} F_C(V^0,\ldots ,V^m)\,\triangleq \,F_init (V^0) \wedge \bigwedge _{t\in [m]}F_{\textbf{G} ^{t}}(V^t,V^{t+1}). \end{aligned}$$
(4)

The satisfying assignments of our encoding will represent weighted Pauli strings, so that we can get the density operator at time m by ranging over satisfying assignments \(\alpha \in SAT(F_C)\):

$$\begin{aligned} \rho ^m = \sum _{\alpha \in SAT(F_C)} F_C(\alpha ) \cdot W(\alpha ) \cdot \bigotimes _{j\in [n]}\sigma [\alpha (x^{m}_{j}),\alpha (z^{m}_{j})], \end{aligned}$$
(5)

where \(W(r^m) = -1\), \(W(\overline{r}^m) = 1\), \(W(u_{t}) = \frac{1}{\sqrt{2}}\), \(W(\overline{u}_{t}) = 1\) for all \(t\in \{0,\ldots ,m\}\) (and all other variables are unbiased). So we will get the weight as \(W(\alpha ) = W(\alpha (r^m))\prod _{t\in [m]}W(\alpha (u_{t}))\). As mentioned before, we only assign weights to \(r^m\) where m is the final time step. We allocate a new \(r^{t+1}\) for each time step t as we always get \(r^{t+1}\) from a constraint related to \(r^t\), but the sign of the final state is given by \(r^m\). So we leave \(r^t\) unbiased except when t is the final time step. Additionally, all the weights are real numbers —there are no complex numbers— enabling the application of a classical weighted model counter that allows negative weights. It is worth noting that in Eq. 2, a generalized density matrix is a linear combination of stabilizers, which can be further decomposed to a sum of weighted Pauli strings. In the encoding, each weighted Pauli string corresponds to a satisfying assignment. In the case of Clifford circuits, since each stabilizer is a single Pauli string, the satisfying assignments can be interpreted as stabilizers. Therefore, for Clifford circuits, there are \(2^n\) satisfying assignments for a n-qubit circuit at each time step. While for Clifford+T circuits, the number of satisfying assignments exceeds \(2^n\).

Example 6

Reconsider Example 4, after solving the constraint \(F_{init} (V^0)\wedge H_0^0\wedge CX_{0,1}^1 \wedge T_0^2 \wedge CX_{0,1}^3 \wedge H_0^4\), the satisfying assignments encoding \(|\varphi _5\rangle \) will be

$$ \left\{ \begin{aligned} & \{r^5 \rightarrow 0, x^5_0 \rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 1, z^5_1\rightarrow 0, u_2\rightarrow 1\}, \\ & \{r^5\rightarrow 1, x^5_0 \rightarrow 1, x^5_1\rightarrow 0, z^5_0\rightarrow 1, z^5_1\rightarrow 0, u_2\rightarrow 1\}, \\ & \{r^5 \rightarrow 0, x^5_0 \rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 0, z^5_1\rightarrow 1, u_2\rightarrow 0\}, \\ & \{r^5 \rightarrow 0, x^5_0 \rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 1, z^5_1\rightarrow 1, u_2\rightarrow 1\}, \\ & \{r^5 \rightarrow 1, x^5_0 \rightarrow 1, x^5_1\rightarrow 0, z^5_0\rightarrow 1, z^5_1\rightarrow 1, u_2\rightarrow 1\}, \\ & \{r^5 \rightarrow 0, x^5_0\rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 0, z^5_1\rightarrow 0, u_2\rightarrow 0\} \end{aligned} \right\} \equiv \left\{ \begin{aligned} \tfrac{1}{\sqrt{2}} Z \otimes I\\ -\tfrac{1}{\sqrt{2}} Y \otimes I\\ I \otimes Z\\ \tfrac{1}{\sqrt{2}} Z \otimes Z\\ -\tfrac{1}{\sqrt{2}} Y \otimes Z\\ I \otimes I \end{aligned} \right\} $$

where \(W(\overline{r}^5) = 1\), \(W(r^5) = -1\), \(W(u_2) = \frac{\sqrt{2}}{2}\) and \(W(\overline{u}_2) = 1\). Here we omit the satisfying assignments for \(\{r^t,x^t_0,x^t_1,z^t_0,z^t_1 \mid 0\le t\le 4\}\).    \(\triangle \)

3.3 Encoding Arbitrary Rotation Gates

Our encoding can be extended to other non-Clifford gates, which we demonstrate by adding rotation gates \(R_X(\theta )\), \(R_Y(\theta )\), and \(R_Z(\theta )\), where \(\theta \) is an angle in radians. The matrices for these gates are given in Table 4.

Table 4. Lookup table for the action of conjugating Pauli gates by rotation gates.

In particular, we have \(T = \exp (-i\frac{\pi }{8})R_Z(\frac{\pi }{4})\), \(S = \exp (-i\frac{\pi }{4})R_Z(\frac{\pi }{2})\) and \(X = -iR_X(\pi )\), \(Y = -iR_Y(\pi )\), \(Z = -iR_Z(\pi )\). Note however that the stabilizer formalism discards the global phase of a state as it updates stabilizers by conjugation, e.g., \(TPT^\dag = \left( \exp (-i\frac{\pi }{8})R_Z(\frac{\pi }{4})\right) P \left( \exp (-i\frac{\pi }{8})R_Z(\frac{\pi }{4})\right) ^\dag = R_Z(\frac{\pi }{4}) P\, R_Z(\frac{\pi }{4})^\dag \). Based on Table 4, the constraints for rotation gates are as shown below, where we write RX for \(R_X\), RY for \(R_Y\) and RZ for \(R_Z\). We keep the coefficients \(\cos (\theta )\) and \(\sin (\theta )\) by defining the weights of the new variables as \(W(u_{1t}) = \cos (\theta )\), \(W(u_{2t}) = \sin (\theta )\), and \(W(\overline{u}_{1t}) = W(\overline{u}_{2t}) = 1\).

$$\begin{aligned} & RX^t_j \,\triangleq \, {} & {} z^{t+1}_{j} \Longleftrightarrow z^t_{j} ~~\wedge ~~ z^{t}_{j} \vee (x^{t+1}_{j} \Leftrightarrow x^t_{j})~~\wedge ~~ r^{t+1} \Longleftrightarrow r^t \oplus z^t_{j} \lnot x^t_{j} x^{t+1}_{j} ~~\wedge ~~ \\ {} & {} {}&u_{1t} \Longleftrightarrow z^t_{j}(x^t_{j}x^{t+1}_{j}\vee \lnot x^t_{j} \lnot x^{t+1}_{j}) ~~\wedge ~~ u_{2t} \Longleftrightarrow z^t_{j}(\lnot x^t_{j}x^{t+1}_{j}\vee x^t_{j} \lnot x^{t+1}_{j}). \\ & RY^t_j \,\triangleq \, {} & {} (x^{t}_{j} \oplus z^{t}_{j}) \Longleftrightarrow (x^{t+1}_{j} \oplus z^{t+1}_{j}) ~~\wedge ~~ (x^{t}_{j} \oplus \lnot z^{t}_{j}) \Longrightarrow (x^{t}_{j} z^{t}_{j} \Leftrightarrow x^{t+1}_{j} \Leftrightarrow z^{t+1}_{j}) \wedge ~~ \\ {} & {} {}& r^{t+1} \Longleftrightarrow r^t \oplus z^t_{j} z^{t+1}_{j} ~~\wedge ~~ u_{1t} \Longleftrightarrow ( x^{t+1}_{j}z^{t}_{j}\oplus x^{t}_{j}z^{t+1}_{j}) ~~\wedge ~~ \\ {} & {} {}&u_{2t} \Longleftrightarrow (x^{t+1}_{j}x^{t}_{j}\oplus z^{t+1}_{j}z^{t}_{j}). \\ & RZ^t_j \,\triangleq \, {} & {} x^{t+1}_{j} \Longleftrightarrow x^t_{j} ~~\wedge ~~ x^{t}_{j} \vee (z^{t+1}_{j} \Leftrightarrow z^t_{j})~~\wedge ~~ r^{t+1} \Longleftrightarrow r^t \oplus x^t_{j} z^t_{j} \lnot z^{t+1}_{j} ~~\wedge ~~ \\ {} & {} {}&u_{1t} \Longleftrightarrow x^t_{j}(z^t_{j}z^{t+1}_{j}\vee \lnot z^t_{j} \lnot z^{t+1}_{j}) ~~\wedge ~~ u_{2t} \Longleftrightarrow x^t_{j}(\lnot z^t_{j}z^{t+1}_{j}\vee z^t_{j} \lnot z^{t+1}_{j}). \end{aligned}$$

3.4 Measurement

We now consider projective measurement both on a single qubit and on multiple qubits of a quantum system. Single-qubit measurement [43] can be used to extract only one bit of information from a n-qubits quantum state, effectively protecting quantum information [57]. It is also used in random quantum circuits, contributing to the study of quantum many-body physics [31]. Measurement on multiple qubits is generally used in quantum algorithms, such as in Grover and Shor algorithms, to get the final result. We implement both measurements using Pauli measurement, where projectors are Pauli strings.

Single-Qubit Pauli Measurement. Let \(\mathbb {P}_{k,0} = I \otimes \cdots \otimes |0\rangle \langle 0|_k \otimes \cdots \otimes I = \frac{1}{2}(I^{\otimes n} + Z_k)\) and \(\mathbb {P}_{k,1} = I \otimes \cdots \otimes |1\rangle \langle 1|_k \otimes \cdots \otimes I = \frac{1}{2}(I^{\otimes n} - Z_k)\) for \(k\in [n]\). When measuring k-th qubit of a n-qubit state \(|\psi \rangle \) using projectors \(\{\mathbb {P}_{k,0}, \mathbb {P}_{k,1}\}\), we get two possible outcomes: 0 with probability \(p_{k,0}\) and 1 with probability \(p_{k,1}\). It follows that \(p_{k,0} = \textrm{tr}(\mathbb {P}_{k,0}|\psi \rangle \langle \psi |)\), where \(\textrm{tr}\) is the trace mapping [52]. As shown in Eq. 2, the density operator \(|\psi \rangle \langle \psi |\) can be written as a weighted sum of Pauli strings, i.e., \(|\psi \rangle \langle \psi | = \frac{1}{2^n} \sum \lambda _P P\) for \(P \in \textrm{PAULI}_n\), \(\lambda _P\in \mathbb R\). The probabilities \(p_{k,0}\) and \(p_{k,1}\) can be obtained as

$$\begin{aligned} \begin{aligned} p_{k,0} = \textrm{tr}(\mathbb {P}_{k,0} \tfrac{1}{2^{n}}\sum \lambda _P P) = \tfrac{1}{2^{n}}\sum \tfrac{1}{2}(\textrm{tr}(I^{\otimes n} \lambda _P P) + \textrm{tr}(Z_k \lambda _P P)), \\ p_{k,1} = \textrm{tr}(\mathbb {P}_{k,1} \tfrac{1}{2^{n}}\sum \lambda _P P) = \tfrac{1}{2^{n}}\sum \tfrac{1}{2}(\textrm{tr}(I^{\otimes n} \lambda _P P) - \textrm{tr}(Z_k \lambda _P P)). \end{aligned} \end{aligned}$$
(6)

In Eq. 6, the trace \(\textrm{tr}(I^{\otimes n} P)\) (resp. \(\textrm{tr}(Z_k P)\)) is non-zero if and only if \(P = \lambda _P I^{\otimes n}\) (resp. \(P = \lambda _P Z_k\)) where \(\lambda _P\in \mathbb {R}\). This follows from two facts: First, given an n-qubit Pauli string \(P= P_0\otimes \ldots \otimes P_{n-1}\), where \(P_i\) are Pauli matrices, the trace \(\textrm{tr}(P) = \textrm{tr}(P_0)\cdots \textrm{tr}(P_{n-1})\) is non-zero if and only if \(P = I^{\otimes n}\), since Pauli matrices are traceless, i.e., \(\textrm{tr}(X) = \textrm{tr}(Y) = \textrm{tr}(Z) = 0\). Second, given \(P_1, P_2 \in \textrm{PAULI}_n\) we have \(P_1 P_2 = I^{\otimes n}\) if and only if \(P_1 = P_2\). Together with the fact that \(\textrm{tr}(I^{\otimes n}) = 2^n\), we can now simplify Eq. 6 to:

$$\begin{aligned} \begin{aligned} p_{k,0} &= \frac{1}{2^{n}}\sum \tfrac{1}{2}(\textrm{tr}(I^{\otimes n} \lambda _P P) + \textrm{tr}(Z_k \lambda _P P)) = \frac{1}{2} (\lambda _{I^{\otimes n}} + \lambda _{Z_k}) \end{aligned} \end{aligned}$$
(7)

Similarly, we have \(p_{k,1} = \frac{1}{2} (\lambda _{I^{\otimes n}} - \lambda _{Z_k})\). In other words, to get the probability of obtaining outcome 0 when measuring the k-th qubit, we need to sum up the weights of all elements:

$$ Z_k \equiv z_k \wedge \bigwedge _{j\in [n]} \overline{x}_j \wedge \bigwedge _{j\in [n], j\ne k} \overline{z}_j ~~ \text { and } ~~ I^{\otimes n} \equiv \bigwedge _{j\in [n]} \overline{x}_j \wedge \bigwedge _{j\in [n]} \overline{z}_j $$

in the flattened Pauli strings of Eq. 2. Thus the measurement can be encoded as follows, for the final time step m.

$$\begin{aligned} F_{\mathbb {P}_{k,b}}(V^m) \,\triangleq \,w \wedge \bigwedge _{j\in [n]} \overline{x}^m_j \wedge \bigwedge _{j\in [n], j\ne k} \overline{z}^m_j , \end{aligned}$$
(8)

where \(W(w) = \frac{1}{2}\) to represent the constant factor in \(p_{k,0}\) and the weight of \(z_k\) is unbiased when \(b=0\) or \(W(z^m_k)=-1\), \(W(\overline{z}^m_k)=1\) when \(b=1\).

A Clifford+T circuit with a single-qubit Pauli measurement at the end can be encoded by conjoining the constraint of initial state and gates in Eq. 4 with the one for the measurement at the end, as Example 7 illustrates.

Example 7

Consider the circuit in Example 6 and assume we want to perform single-qubit Pauli measurement on the first qubit using \(\{\frac{I\otimes I+Z \otimes I}{2}, \frac{I\otimes I-Z \otimes I}{2}\}\).

figure h

By adding the measurement constraint \(F_{\mathbb {P}_{0,0}} \,\triangleq \,w \wedge \overline{x}^5_0 \wedge \overline{x}^5_1 \wedge \overline{z}^5_1\) to the circuits constraints in Example 6, we get \(F_{init}(V^0)\wedge H_0^0\wedge CX^1_{0,1} \wedge T_0^2 \wedge CX_{0,1}^3\wedge H_0^4 \wedge F_{\mathbb {P}_{0,0}}(V^5)\). The satisfying assignments will be the subset of the solutions in Example 6 with the constant variable w:

$$ \left\{ \begin{aligned} & \{r^5 \rightarrow 0, x^5_0 \rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 1, z^5_1\rightarrow 0, u_2\rightarrow 1, w\rightarrow 1\}, \\ & \{r^5 \rightarrow 0, x^5_0\rightarrow 0, x^5_1\rightarrow 0, z^5_0\rightarrow 0, z^5_1\rightarrow 0, u_2\rightarrow 0, w \rightarrow 1\} \end{aligned} \right\} \equiv \left\{ \begin{aligned} \tfrac{1}{2\sqrt{2}} Z \otimes I\\ \tfrac{1}{2}I \otimes I \end{aligned} \right\} $$

where we only show the variables in \(V^5\) representing the final state. The resulting probability is \(W(\overline{r}^5)W(u_2)W(w) + W(\overline{r}^5)W(\overline{u}_2)W(w) = \frac{1}{2\sqrt{2}} + \frac{1}{2}\).    \(\triangle \)

Multi-qubit Pauli Measurement. Similar to the single-qubit Pauli measurement, we can resolve the constraint of n-qubit Pauli measurement based on the measurement projector \(\mathbb {P}\). Given a quantum state \(|\psi \rangle \langle \psi |=\frac{1}{2^n}\sum _{P} \lambda _P P\), let \({Q}\subseteq [n]\) be the set of qubits being measured. Without loss of generality, we explain how to obtain the probability of obtaining the outcome zero (0) for all qubits \(q \in Q\). The projector measuring qubits in Q is defined as \(\mathbb {P}_{{Q}} \,\triangleq \,\bigotimes _{q\in Q}P_q\) where \(P_q=(I+Z)/2\) for \(q\in {Q}\) and \(P_q=I\) for \(q\in [n]\setminus {Q}\). We can derive the constraint \(F_{\mathbb {P}_q}(x_q^m,z_q^m) = \overline{x}_q^m\) for \(q\in {Q}\) and no constraint for \(q\in [n]\setminus {Q}\). By conjoining them, we obtain the measurement constraint \(F_{\mathbb {P}_{{Q}}} = w \wedge \bigwedge _{q\in [n]}\overline{x}_q^m \wedge \bigwedge _{q\in [n]\setminus {Q}}\overline{z}_q^m \) where w is a constant variable for the factor and \(W(w) = \frac{1}{2^{|Q|}}\).

We conclude Sect. 3 with Proposition 2, which also shows that our encoding implements a strong simulation of a universal quantum circuit.

Proposition 2

Given an n-qubit quantum circuit C, a subset of qubits \({Q}\subseteq [n]\) and the WMC encoding of the corresponding simulation problem \(F(V^0,\ldots ,V^{m}) = F_C(V^0,\ldots ,V^{m})\wedge F_{\mathbb {P}_{{Q}}}(V^{m})\) with according weight function W, the weighted model count of F equals the probability of the measurement outcome corresponding to \(\mathbb {P}_{Q}\) (outcome 0 for all qubits \(q\in Q\)) on circuit C, i.e., \(MC_W(F) = \langle 0|^{\otimes n}C^\dag \mathbb {P}_{Q} C|0\rangle ^{\otimes n}\).

4 Experiments

To show the effectiveness of our approach, we implemented a WMC-based simulator in a tool called QCMC. It reads quantum circuits in QASM format [26], encodes them to Boolean formulas in conjunctive normal form (CNF) as explained in Sect. 3, and then uses the weighted model counter GPMC [36] to solve these constraints. We choose GPMC as it is the best solver supporting negative weights in model counting competition 2023 [4]. The resulting implementation and evaluation are publicly available at [46].

We compare our method against two state-of-the-art tools: QuiZX [41] based on ZX calculus [25] and Quasimodo [64] based on CFLOBDD [65]. In particular, this empirical analysis is performed on two families of circuits: (i) random Clifford+T circuits, which mimic hard problems arising in quantum chemistry [75] and quantum many-body physics [31]; (ii) random circuits mimicking oracle implementations; (iii) all benchmarks from the public benchmark suite MQT Bench [58], which includes many important quantum algorithms like QAOA, VQE, QNN and Grover. All experiments have been conducted on a 3.5 GHz M2 Machine with MacOS 13 and 16 GB RAM. We set the time limit to be 5 min (300 s) and include the time to read a QASM file, construct the weighted CNF, and perform the model counting in all reported runtimes.

4.1 Results

First, we show the limits of three methods using the set of benchmarks generated by [41]. They construct random circuits with a given number of T gates by exponentiating Pauli unitaries in the form of \(\exp (-i(2k+1)\frac{\pi }{4}P)\) where P is a Pauli string and \(k\in \{1,2\}\). We reuse their experimental settings, which gradually increase the T count (through Pauli exponentiation) for \(n =50\) qubits, and we add an experiment with \(n = 100\) qubits. Accordingly, we generate 50 circuits with different random seeds for each \(n = 50\) and \(T \in [0\text {--}100]\) and each \(n = 100\) and \(T \in [0\text {--}180]\). We then perform a single-qubit Pauli measurement on the first qubit. We plot the minimal time needed and the rate of successfully getting the answer in 5 min among all 50 simulation runs in Fig. 1.

Fig. 1.
figure 1

The upper two figures, are percentages of random 50- and 100-qubit circuits with increasing depth which can be successfully measured in 5 min. The below two figures, both of which have y-axes on a logarithmic scale, are the minimum running time among the 50 samples for each configuration.

Second, we also consider random circuits that more resemble typical oracle implementations — random quantum circuits with varying qubits and depths, which comprise the CX, H, S, and T gates with appearing ratio 10%, 35%, 35%, 20% [56]. The resulting runtimes can be seen in Fig. 2.

Fig. 2.
figure 2

Computational basis measurement of typical random Clifford+T circuits. (Both vertical axes are on a logarithmic scale.) CFLOBDD runs out of time for all benchmarks so we do not add it here.

In addition to the random circuits, we empirically evaluated our method on the MQTBench benchmark set [58], measuring all qubits, as is typical in most quantum algorithms. We present a representative subset of results in Table 5. The complete results can be found in [47]. All benchmarks are expanded to the Clifford+T+R gate set, where R denotes \(\{R_X,R_Y,R_Z\}\). The first two columns list the number of qubits n and the number of gates |G|. Columns T and R give the number of T gates and rotation gates. Then, the performance of the WMC-based tool QCMC, the performance of the ZX calculus-based tool QuiZX (ZX), and the performance of CFLOBDD-based tool Quasimodo (CFLOBDD). The performances are given by the runtime and the corresponding memory usage. Given that the variation in each run is minimal, we present the results of a single run for all experiments.

4.2 Discussion

For all cases, QuiZX gives algebraic answers while the QCMC and CFLOBDD methods give numerical answers. Because of the imprecision of floating-point arithmetic, we consider a equal to b if \(|a-b|<10^{-8}\). With this equality tolerance, all three methods produce the same answers.

For random circuits, Fig. 1 illustrates that the minimum runtime barely increases for QCMC, while it seems exponential for QuiZX (note the log scale). However, when the number of qubits is \(n = 50\) and the T count is larger than 70, or when, \(n = 100\) and the T count is larger than 110, QuiZX has a better success rate, i.e., it completes more simulations than QCMC in 5 min. In contrast, CFLOBDD exhibits the lowest success rate among the three methods. When it comes to a typical random Clifford+T circuit Fig. 2 shows that the runtime of both QCMC and ZX exhibits a clear correlation with the size of the circuits, while CFLOBDD can not solve all benchmarks in 5 min. The proposed implementation consistently outperforms QuiZX by one to three orders of magnitude especially when the size is getting larger (note again the log scale). However, the story changes when considering structural quantum circuits.

Table 5. Results of verifying circuits from MQT bench. For cases within time limit, we give their running time (sec) and corresponding memory usage (MB). We use ✕ when QuiZX does not support certain benchmarks, while \(>300\) represents a timeout (5 min). For those benchmarks having a timeout or are not supported, we omit their memory usage by \(\star \).

For MQT benchmarks, Table 5 shows that QCMC performs better than QuiZX except for GHZ state and Graph State where QCMC is slightly slower in milliseconds. CFLOBDD significantly surpasses QCMC on Grover and quantum walk algorithms, primarily due to the decision diagram-based method’s proficiency in handling circuits featuring large reversible parts and oracles. While for those circuits featuring a large number of rotation gates with various rotation angles, like Graph state, QFT, and VQE, QCMC demonstrates clear advantages. This distinction arises from the fact that when dealing with rotation gates, it might happen that two decision diagram nodes that should be identical in theory, differ by a small margin in practice, obstructing node merging [53]. In contrast, the WMC approach —also numerical in nature— avoids explicit representation of all satisfying assignments, by iteratively computing a sum of products. This not only avoids blowups in space use but, we hypothesize, also avoids numerical instability, a problem that has plagued numerical decision-diagram based approaches [53, 56]. In terms of memory usage, CFLOBDD always uses more than 340 MB, in some cases uses more than 6 GB (graph state, \(n = 64\)), while QCMC and QuiZX use less than 13 MB (OS reported peak resident set size).

Overall, both QCMC and QuiZX outperform CFLOBDD in handling random circuits. Moreover, QCMC has better runtime performance than QuiZX. For structural circuits, QuiZX faces a limitation as it does not efficiently support rotation gates with arbitrary angles, so it is incapable of simulating many quantum algorithms, like VQE, directly. In terms of runtime, CFLOBDD is better at circuits featuring structure, while QCMC performs better at circuits with arbitrary rotation gates. However, CFLOBDD has a significantly higher memory cost compared to both QCMC and QuiZX.

5 Related Work

In this section, we give an overview of the related work on classical simulation of quantum computing with a focus on those methods applying SAT-based solvers.

SAT-based solvers have proven successful in navigating the huge search spaces encountered in various problems in quantum computing [49, 73], initial attempts have been made to harness the strengths of satisfiability solvers for the simulation of quantum circuits. For instance, [11] implements a simulator for Clifford circuits based on a SAT encoding (our encoding of HSCX in Eq. 3 is similar to theirs). The authors also discuss a SAT encoding for universal quantum circuits, which however requires exponentially large representations, making it impractical. Besides quantum circuits, [10] presents symQV, a framework for verifying quantum programs in a quantum circuit model, which allows the encoding of the verification problem in an SMT formula, which can then be checked with a \(\delta \)-complete decision process. There is an SMT theory for quantum computing [23].

Another method is based on decision diagrams (DDs) [1, 16], which represent many Boolean functions succinctly, while allowing manipulation operations without decompression. DD methods for pseudo-Boolean functions include Algebraic DDs (ADD) [9, 24, 70] and various “edge-valued” ADDs [44, 61, 67, 74]. The application of DDs to quantum circuit simulation, by viewing a quantum state as a pseudo-Boolean function, was pioneered with QuiDDs [71] and further developed with Quantum Multi-valued DDs [50], Tensor DDs [38] and CFLOBDDs [65]. All but CFLOBDD are essentially ADDs with complex numbers.

Another way is to translate quantum circuits into ZX-diagrams [25], which is a graphical calculus for quantum circuits equipped with powerful rewrite rules.

Classical simulation is commonly used for the verification of quantum circuits, with extensive research focused on their equivalence checking [5, 7, 72]. It can also be applied to bug hunting in quantum circuits. In [22], the authors proposed a tree automata to compactly represent quantum states and gates algebraically, framing the verification problem as a Hoare triple.

6 Conclusions

In this work, we proposed a generalized stabilizer formalism formulated in terms of a stabilizer group. Based on this, we provided an encoding for various universal gate sets as a weighted model counting problem with only real weights, obviating the need for complex numbers that are not supported by existing WMC tools. Besides T gates, we also extended our encoding to general rotation gates. Furthermore, we demonstrated how to perform computational basis measurements using this encoding, enabling strong quantum circuit simulation.

We have implemented our method in an open-source tool QCMC. To give empirical results on the practicality of our method, we have applied it to a variety of benchmarks comparing one based on ZX-calculus and one based on decision diagrams. Experimental results show that our approach outperforms both in several cases, particularly with circuits of large sizes. The performance of our approach is quite different from the other approaches, demonstrating the unique potential of WMC in various use cases.

This work provides a new benchmark paradigm for weighted model counting problems. It would be interesting to see whether WMC tools can be improved for this novel application domain. In the future, it could also be worthwhile to apply our encoding to approximate weighted model counting [19, 29]. Moreover, using weighted samplers [34, 45], we could achieve weak circuit simulation with the same encoding. The main obstacle now is incorporating negative weights into approximate weighted model counters and samplers. Additionally, it would be valuable to explore more applications of this encoding, such as checking the equivalence of two quantum circuits and entanglement purification.