Qsim and Cirq
Cirq, developed by Google, can simulate circuits on a real-world qubit architecture. It also has multiple simulators included in its toolkit, e.g., Qsim. Qsim is faster than the default Cirq simulator. Cirq is a Python library for creating, modifying, and optimizing quantum circuits. It is also possible to execute your code on real quantum devices or simulators.
The additional learning materials are available on Cirq and Qsim.
Getting Started
Below is a base example for creating and running the Cirq
and Qsim
simulator:
import cirq
# Create a circuit to generate a Bell State (1/sqrt(2) * ( |00⟩ + |11⟩ ))
bell_circuit = cirq.Circuit()
# Create two qubits (q0 and q1)
q0, q1 = cirq.LineQubit.range(2)
# Apply a Hadamard gate to q0 (creates superposition)
bell_circuit.append(cirq.H(q0))
# Apply a CNOT gate with q0 as control and q1 as target (creates entanglement)
bell_circuit.append(cirq.CNOT(q0, q1))
# Initialize the simulator
s = cirq.Simulator()
# Simulate the circuit (without measurement, gives the quantum state)
print('Simulating the circuit:')
results = s.simulate(bell_circuit)
print(results)
# Add a measurement gate to both qubits to observe their values
bell_circuit.append(cirq.measure(q0, q1, key='result'))
# Sample the circuit 1000 times (to observe measurement statistics)
samples = s.run(bell_circuit, repetitions=1000)
# Plot the measurement results using a histogram
import matplotlib.pyplot as plt
cirq.plot_state_histogram(samples, plt.subplot())
plt.show()
Code Insights
- Import the necessary dependencies.
- Define a quantum state and build the quantum circuit.
- Hints between the lines:
- The
s.run
is sampling the circuit with respect to therepetitions
, to get a sample distribution of measurements which is graphed bycirq.plot_state_histogram()
.
- The
- Switching to Qsim.
- To run the code in
Qsim
, the following changes need to be done:
import qsimcirq . . . # Initialize Simulator s = qsimcirq.QSimSimulator() . . .
- To run the code in
Follow up on the GWDG Updates:
- Cirq
- The version of Cirq provided by GWDG: v1.4.1
- Qsim
- The version of Qsim provided by GWDG: v0.21.0
Key Features
Cirq is an open-source framework designed for building and running quantum circuits on near-term quantum devices. Together with Qsim, Google’s advanced quantum circuit simulator, Cirq provides a comprehensive platform for developing, testing, and simulating quantum algorithms.
Circuit Design for NISQ Devices: Cirq enables users to customize circuits with particular hardware limitations and noise models.
Qsim: Qsim is a high-performance simulation tool that effectively simulates quantum circuits on classical hardware, and it is an extension of Cirq. Its efficient enhancements enable the speedy processing of extensive circuits and intricate algorithms.
Customized Noise Models: Cirq allows for the creation of customized noise models, aiding users in accurately replicating the performance of actual quantum devices.
Integration with Google`s Quantum Hardware: Cirq smoothly combines with Google’s quantum processors, enabling users to run quantum circuits on both simulated and actual hardware using the same platform.
Supplementary Modules
Command | Description |
---|---|
cirq.StatevectorSimulator() | Simulator that computes the exact evolution of a quantum system using state vectors. |
cirq.DensityMatrixSimulator | Simulates the mixed states. |
cirq.phase_flip | Flips the phase of a qubit. |
cirq.phase_damp | Applies a phase damping channel to a qubit. |
cirq.amplitude_damp | Applies an amplitude damping channel to a qubit. |
cirq.depolarize | Applies a depolarizing noise channel to a qubit. |
cirq.asymmetric_depolarize | Applies an asymmetric depolarizing noise channel to a qubit. |
cirq.reset | Resets a qubit to its ground state. |
cirq.Gate | Represents a quantum gate. |
cirq.Operation | Represents a quantum operation on a qubit. |
cirq.Moment | Represents a moment in a quantum circuit. |
cirq.ParamResolver | Resolves parameters in a quantum circuit. |
There are many options for different applications that can be found easily on Cirq documentation such as GPU/CPU based simulations, noisy simulation, exact simulation, parameter sweeps, state histograms.