Quantum Development Kit (QDK) sparse simulator
The sparse simulator is a simulator that utilizes a sparse representation of quantum state vectors, as opposed to the full-state simulator. This feature allows the sparse simulator to minimize the memory footprint used to represent quantum states, thus enabling simulations over a larger number of qubits. The sparse simulator is efficient for representing quantum states that are sparse in the computational basis, that is, quantum states for which most of the amplitude coefficients are zero in the computational basis. As such, sparse simulator enables users to explore larger applications than what can be represented using the full-state simulator which will waste both memory and time on an exponentially large number of zero-amplitudes.
For more information about the sparse simulator, please see Jaques and Häner (arXiv:2105.01533).
Invoking and running the sparse simulator
The sparse simulator is exposed via the SparseSimulator class. For more information, see Ways to run a Q# program.
Invoking the simulator from C#
Create an instance of the SparseSimulator class and then pass it to the Run method of a quantum operation, along with any parameters.
using (var sim = new SparseSimulator())
{
var res = RunMyOperation.Run(sim).Result;
///...
}
Because the SparseSimulator class implements the IDisposable interface, you must call the Dispose method once you don't need the instance of the simulator anymore. The best way to automate that call is to wrap the simulator declaration and operations within a using statement, which automatically calls the Dispose method.
For an example of sparse simulator usage in C# see the Integer Factorization Sample.
Simulator options
The behavior of the sparse simulator can be adjusted via the following parameters to the C# constructor:
throwOnReleasingQubitsNotInZeroState: The simulator can warn you by throwing an exception if qubits haven't been returned to thezerostate before release. Resetting or measuring qubits before release is required by the Q# spec - not doing so may lead to computational errors! The default istrue.randomNumberGeneratorSeed: Obtain deterministic behavior by seeding the simulator as described above.disableBorrowing: If you don't want to use borrowed qubits for this simulation, you can disable this feature by setting this parameter totrue. Borrowed qubits will instead be replaced with regular clean qubits. The default isfalse.numQubits: The number of qubits the sparse simulator will be operating with. By default, this number is set to 64 and it has a hard limit of 1024.
The code below shows a possible configuration of the parameters.
var sim = new SparseSimulator(
throwOnReleasingQubitsNotInZeroState: false,
randomNumberGeneratorSeed: 42,
disableBorrowing: true,
numQubits: 73
)
Invoking the simulator from Python
Use the simulate_sparse() method from the Q# Python library with the imported Q# operation:
qubit_result = RunMyOperation.simulate_sparse()
Invoking the simulator from the command line
You can use the --simulator (or -s shortcut) parameter to specify the desired target machine.
dotnet run -s SparseSimulator
Invoking the simulator from Jupyter Notebooks
Use the IQ# magic command %simulate_sparse to run the Q# operation.
%simulate_sparse RunMyOperation
For an example of sparse simulator usage in Jupyter Notebook see the LargeSimulation Sample.
Seeding the simulator
By default, the sparse simulator uses a random number generator to simulate quantum randomness. For testing purposes, it's sometimes useful to have deterministic results. In a C# program, you can accomplish this determinism by providing a seed for the random number generator in the SparseSimulator constructor via the randomNumberGeneratorSeed parameter.
using (var sim = new SparseSimulator(randomNumberGeneratorSeed: 42))
{
var res = RunMyTestOperation.Run(sim).Result;
///...
}
See also
Povratne informacije
Pošalјite i prikažite povratne informacije za