### Test qsimcirq Installation Source: https://quantumai.google/qsim/install_qsimcirq After installation, copy the provided test file to your machine and run pytest to verify the installation. This command also serves as an example of how to use qsimcirq. ```bash python3 -m pytest qsimcirq_test.py ``` -------------------------------- ### Install Cirq and qsimcirq Source: https://quantumai.google/qsim/tutorials/qsimcirq Installs the necessary Cirq and qsimcirq packages. It checks for existing installations and installs them quietly if not found. ```python import time try: import cirq except ImportError: !pip install cirq --quiet import cirq try: import qsimcirq except ImportError: !pip install qsimcirq --quiet import qsimcirq ``` -------------------------------- ### Install Cirq and qsimcirq Source: https://quantumai.google/qsim/tutorials/noisy_qsimcirq Installs the necessary Cirq and qsimcirq packages if they are not already present. This is a prerequisite for running the tutorial. ```python try: import cirq except ImportError: !pip install cirq --quiet import cirq try: import qsimcirq except ImportError: !pip install qsimcirq --quiet import qsimcirq ``` -------------------------------- ### Verify qsim GPU Installation Source: https://quantumai.google/qsim/tutorials/gcp_gpu Verifies the qsim installation by importing the qsim_gpu module. A successful import indicates that the GPU-enabled version was compiled and installed correctly. ```python python3 -c "import qsimcirq; print(qsimcirq.qsim_gpu)" ``` -------------------------------- ### Start qsim Docker container Source: https://quantumai.google/qsim/tutorials/gcp_cpu Run this command on your VM to start the qsim Docker container. It mounts the current directory to `/homedir` inside the container and maps port 8888. The `&` runs the container in the background. ```bash docker run -v `pwd`:/homedir -p 8888:8888 gcr.io/quantum-builds/github.com/quantumlib/jupyter_qsim:latest & ``` -------------------------------- ### X Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an X gate to a qubit. ```text 2 x 2 ``` -------------------------------- ### S Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an S gate to a qubit. ```text 2 s 1 ``` -------------------------------- ### Install Build Tools for qsim Source: https://quantumai.google/qsim/tutorials/gcp_gpu Installs CMake, pip, and pybind11, which are required to build the qsim library. This command ensures all necessary development tools are present. ```bash sudo apt install cmake && sudo apt install pip && pip install pybind11 ``` -------------------------------- ### Install Development Dependencies Source: https://quantumai.google/qsim/install_qsimcirq Use these commands to install development dependencies if you plan to develop qsimcirq. Ensure you have pip version 25.1 or higher. ```bash pip install -r requirements.txt ``` ```bash pip install --group dev ``` -------------------------------- ### Verify qsim Installation and Run Simulation Source: https://quantumai.google/qsim/tutorials/gcp_cpu This Python code verifies your qsim installation by creating a simple quantum circuit, simulating it using qsimcirq, and printing the results. Paste this code into the REPL or a file within your connected container. ```python # Import Cirq and qsim import cirq import qsimcirq # Instantiate qubits and create a circuit q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CX(q0, q1)) # Instantiate a simulator that uses the GPU qsim_simulator = qsimcirq.QSimSimulator() # Run the simulation print("Running simulation for the following circuit:") print(circuit) qsim_results = qsim_simulator.compute_amplitudes( circuit, bitstrings=[0b00, 0b01]) print("qsim results:") print(qsim_results) ``` -------------------------------- ### Install qsimcirq and Cirq Source: https://quantumai.google/qsim/qsimcirq_tutorial Install the necessary libraries for using qsimcirq with Cirq. Ensure you are using compatible versions. ```shell %%shell pip install cirq==0.8.2 --quiet pip install qsimcirq==0.3.0 --quiet ``` -------------------------------- ### Install Cirq and qsimcirq Source: https://quantumai.google/qsim/tutorials/q32d14 Installs the necessary Cirq and qsimcirq packages. Includes checks to avoid reinstalling if already present. ```python import time import numpy as np try: import cirq except ImportError: !pip install cirq --quiet import cirq try: import qsimcirq except ImportError: !pip install qsimcirq --quiet import qsimcirq ``` -------------------------------- ### QSimSimulator Execution Output Source: https://quantumai.google/qsim/tutorials/q32d14 Example output from the QSimSimulator, showing computed amplitudes and execution times. ```text Result: [(-3.869717602356104e-06-5.219020749791525e-06j), (7.610328793816734e-06+9.70489600149449e-06j)] Wall Time: 147.6608045989999 -- Process Time: 2295.0433410680002 ``` -------------------------------- ### Install CUDA Toolkit Source: https://quantumai.google/qsim/tutorials/gcp_gpu Installs the NVIDIA CUDA toolkit, which is necessary for GPU acceleration. This command should be run on the virtual machine. ```bash sudo apt install -y nvidia-cuda-toolkit ``` -------------------------------- ### Example gcloud Configuration Output Source: https://quantumai.google/qsim/tutorials/qsimcirq_gcp An example of the output from `gcloud config list`, showing the active region, zone, account, and project settings. ```text region = us-east4 zone = us-east4-c [core] account = wellhello@gargle.com disable_usage_reporting = False project = quantum-22222 ``` -------------------------------- ### Handling Intermediate Measurements with qsimcirq Source: https://quantumai.google/qsim/tutorials/qsimcirq Demonstrates simulating a circuit with intermediate measurements using qsimcirq's QSimSimulator. This example shows how qsim correctly handles circuits where measurements occur before the circuit's end, ensuring accurate sampling for each repetition. ```python # Define a circuit with intermediate measurements. q0 = cirq.LineQubit(0) circuit = cirq.Circuit( cirq.X(q0) ** 0.5, cirq.measure(q0, key="m0"), cirq.X(q0) ** 0.5, cirq.measure(q0, key="m1"), cirq.X(q0) ** 0.5, cirq.measure(q0, key="m2"), ) print("Circuit:") print(circuit) print() # Simulate the circuit with qsim and return just the measurement values. print("qsim results:") qsim_simulator = qsimcirq.QSimSimulator() qsim_results = qsim_simulator.run(circuit, repetitions=5) print(qsim_results) ``` ```text Circuit: 0: ───X^0.5───M('m0')───X^0.5───M('m1')───X^0.5───M('m2')─── qsim results: m0=00110 m1=11111 m2=10001 ``` -------------------------------- ### Install qsimcirq on Linux Source: https://quantumai.google/qsim/install_qsimcirq Install the qsimcirq Python package on Linux. This command automatically checks for CUDA and GPUs and attempts to build a GPU-accelerated version if available. ```bash pip3 install qsimcirq ``` -------------------------------- ### Square Root of W Gate Example Source: https://quantumai.google/qsim/input_format Example of applying the square root of W gate (√W), equivalent to Rx,y(π/4, π/2), to a qubit. ```text 1 hz_1_2 1 ``` -------------------------------- ### Verify qsim GPU Usage with Cirq Source: https://quantumai.google/qsim/tutorials/gcp_gpu This code snippet demonstrates how to use qsim with GPU acceleration. It sets up a simple circuit and runs it using a QSimSimulator configured for GPU. Adjust 'mode' or 'num_gpus' based on your setup (Option 1/2 vs. Option 3). ```python # Import Cirq and qsim import cirq import qsimcirq # Instantiate qubits and create a circuit q0, q1 = cirq.LineQubit.range(2) circuit = cirq.Circuit(cirq.H(q0), cirq.CX(q0, q1)) # Instantiate a simulator that uses the GPU # Option 1 (mode=0) or Option 2 (mode=1) gpu_options = qsimcirq.QSimOptions(use_gpu=True, gpu_mode = mode, max_fused_gate_size=4) # Option 3 (number of GPUs = `num_gpus`) gpu_options = qsimcirq.QSimOptions(disable_gpu=False, gpu_mode = num_gpus, max_fused_gate_size=4) qsim_simulator = qsimcirq.QSimSimulator(qsim_options=gpu_options) # Run the simulation print("Running simulation for the following circuit:") print(circuit) qsim_results = qsim_simulator.compute_amplitudes( circuit, bitstrings=[0b00, 0b01]) print("qsim results:") print(qsim_results) ``` -------------------------------- ### Navigate to Tutorial Directory Source: https://quantumai.google/qsim/tutorials/multinode Change the current directory to the multinode tutorial directory within the cloned qsim repository. ```bash cd qsim/docs/tutorials/multinode ``` -------------------------------- ### Simulate Quantum Circuit with QSimSimulator Source: https://quantumai.google/qsim/tutorials/qsimcirq_gcp Instantiate the QSimSimulator and use it to simulate the previously defined quantum circuit. This demonstrates running a simulation on your VM. ```python simulator = qsimcirq.QSimSimulator() result = simulator.simulate(circuit) print("Result:") print(result) ``` -------------------------------- ### Run Sample Simulation with AVX and OpenMP Source: https://quantumai.google/qsim/bazel Execute a sample simulation using the `apps:qsim_base` target with AVX and OpenMP optimizations. Ensure the `circuit_q24` file is specified correctly. ```bash bazel run --config=avx --config=openmp apps:qsim_base -- -c circuits/circuit_q24 ``` -------------------------------- ### Y Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a Y gate to a qubit. ```text 3 y 3 ``` -------------------------------- ### T Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a T gate to a qubit. ```text 1 t 1 ``` -------------------------------- ### Configure QSimSimulator Options Source: https://quantumai.google/qsim/cirq_interface Instantiate QSimSimulator with custom options for CPU threads, verbosity, and denormal number handling. ```python qsim_options = qsimcirq.QSimOptions(cpu_threads=8, verbosity=0, denormals_are_zeros=True) my_sim = qsimcirq.QSimSimulator(qsim_options) myres = my_sim.simulate(program=my_circuit) ``` -------------------------------- ### Z Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a Z gate to a qubit. ```text 4 z 4 ``` -------------------------------- ### Basic Bazel Test Run Source: https://quantumai.google/qsim/bazel Run all qsim tests with the default build configuration. This is the basic command for building and testing. ```bash bazel test tests:all ``` -------------------------------- ### Hadamard Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a Hadamard gate to a qubit. ```text 0 h 0 ``` -------------------------------- ### Initialize QSimSimulator with GPU options Source: https://quantumai.google/qsim/tutorials/amd_gpu Instantiate the QSimSimulator for AMD Instinct GPUs. Ensure `use_gpu` is true and `gpu_mode` is set to 0. ```python simulator = qsimcirq.QSimSimulator(qsim_options=qsimcirq.QSimOptions( use_gpu=True, gpu_mode=0, ... )) ``` -------------------------------- ### Identity (2-qubit) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a 2-qubit Identity gate. ```text 8 id2 0 1 ``` -------------------------------- ### Identity (1-qubit) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a 1-qubit Identity gate. ```text 7 id1 0 ``` -------------------------------- ### Compute Amplitudes with QSimhSimulator (Initial Options) Source: https://quantumai.google/qsim/qsimcirq_tutorial Initializes the QSimhSimulator with specified options and computes amplitudes for a given circuit and bitstrings. Useful for exploring specific simulation configurations. ```python qsimh_simulator = qsimcirq.QSimhSimulator(options) results_0 = qsimh_simulator.compute_amplitudes( circuit, bitstrings=[0b00, 0b01, 0b10, 0b11]) print(results_0) ``` -------------------------------- ### iSwap Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an iSwap gate to two qubits. ```text 4 is 6 7 ``` -------------------------------- ### CNOT Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a CNOT gate to two qubits. ```text 3 cnot 4 5 ``` -------------------------------- ### Initialize Google Cloud SDK Source: https://quantumai.google/qsim/tutorials/qsimcirq_gcp Initialize the Google Cloud SDK on your local machine to manage GCP resources and connect to your VM. This command prompts for your project ID and zone. ```bash $ gcloud init ``` -------------------------------- ### CZ Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a CZ gate to two qubits. ```text 2 cz 2 3 ``` -------------------------------- ### Measurement (n-qubit) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a measurement gate to multiple qubits. ```text 9 m 0 1 2 3 ``` -------------------------------- ### Square Root of Y Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a square root of Y gate (√Y) to a qubit. ```text 6 y_1_2 6 ``` -------------------------------- ### Simulate circuit with QSimSimulator options Source: https://quantumai.google/qsim/cirq_interface Instantiate QSimSimulator with custom options for CPU threads and verbosity. The simulate method then executes the defined Cirq circuit. ```python # equivalent to {'t': 8, 'v': 0} qsim_options = qsimcirq.QSimOptions(cpu_threads=8, verbosity=0) my_sim = qsimcirq.QSimSimulator(qsim_options) myres = my_sim.simulate(program=my_circuit) ``` -------------------------------- ### CPhase Gate Example Source: https://quantumai.google/qsim/input_format Example of applying a CPhase gate with a specified angle φ to two qubits. ```text 6 cp 0 1 0.78 ``` -------------------------------- ### Tuning qsim Performance with Options Source: https://quantumai.google/qsim/qsimcirq_tutorial Demonstrates how to tune qsim performance by passing options to the QSimSimulator constructor, such as specifying the number of threads for parallelization. ```python # Use eight threads to parallelize simulation. options = {'t': 8} qsim_simulator = qsimcirq.QSimSimulator(options) qsim_start = time.time() qsim_results = qsim_simulator.simulate(circuit) qsim_elapsed = time.time() - qsim_start print(f'qsim runtime: {qsim_elapsed}') ``` -------------------------------- ### Rz(φ) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an Rz gate with a specified angle φ to a qubit. ```text 9 rz 9 0.79 ``` -------------------------------- ### Ry(φ) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an Ry gate with a specified angle φ to a qubit. ```text 8 ry 8 1.05 ``` -------------------------------- ### Rx(φ) Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an Rx gate with a specified angle φ to a qubit. ```text 7 rx 7 0.79 ``` -------------------------------- ### Run qsim Simulation Binary in Docker Source: https://quantumai.google/qsim/docker Once the qsim Docker image is created, use this command to run the qsim_base.x simulation binary. The -v flag is required to mount host directories into the container for accessing circuit files. ```bash docker run -ti --rm -v $PWD/circuits:/qsim/circuits:ro \ qsim:latest -c /qsim/circuits/circuit_q24 ``` -------------------------------- ### fSim Gate Example Source: https://quantumai.google/qsim/input_format Example of applying an fSim gate with specified angles θ and φ to two qubits. ```text 5 fs 6 7 3.14 1.57 ```