forked from GoogleCloudPlatform/cluster-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantum-circuit-simulator.yaml
139 lines (128 loc) · 5.7 KB
/
quantum-circuit-simulator.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
blueprint_name: quantum-circuit
# Please review https://cloud.google.com/compute/docs/regions-zones
# for availability of A2 machine types
vars:
project_id: ## Set project id here
deployment_name: qsim-demo
region: us-central1
zone: us-central1-f
# Documentation for each of the modules used below can be found at
# https://github.com/GoogleCloudPlatform/hpc-toolkit/blob/main/modules/README.md
deployment_groups:
- group: primary
modules:
- id: network1
source: modules/network/vpc
- id: quantum-simulator-setup
source: modules/scripts/startup-script
settings:
runners:
- type: shell
destination: install-qsim.sh
content: |
#!/bin/bash
# This script implements https://quantumai.google/qsim/tutorials/gcp_gpu
set -e -o pipefail
curl -O https://raw.githubusercontent.com/GoogleCloudPlatform/compute-gpu-installation/main/linux/install_gpu_driver.py
python3 install_gpu_driver.py
curl -O https://repo.anaconda.com/miniconda/Miniconda3-py39_4.12.0-Linux-x86_64.sh
bash Miniconda3-py39_4.12.0-Linux-x86_64.sh -b -p /opt/conda
source /opt/conda/bin/activate base
conda init --system
# following channel ordering is important! use strict_priority!
# cuquantum comes from cuquantum label in nvidia channel
# libcutensor comes from main (default) label in nvidia channel
# cuda and all toolkit comes from cuda-11.5.2 label in nvidia channel
# everything else comes from conda-forge
conda config --system --set channel_priority strict
conda config --system --remove channels defaults
conda config --system --add channels conda-forge
conda config --system --add channels nvidia
conda config --system --add channels nvidia/label/cuda-11.5.2
conda config --system --add channels nvidia/label/cuquantum-22.07.1
conda update -n base conda --yes
conda create -n qsim python=3.9 --yes
conda install -n qsim cuda cuquantum make cmake cxx-compiler=1.5.1 --yes
echo "cuda ==11.5.*" > /opt/conda/envs/qsim/conda-meta/pinned
conda clean -p -t --yes
conda activate qsim
pip install pybind11 cirq
git clone https://github.com/quantumlib/qsim.git /opt/qsim
cd /opt/qsim
export CUQUANTUM_DIR=/opt/conda/envs/qsim
make
pip install .
- type: data
destination: /var/tmp/qsim-example.py
content: |
import sys
import time
import cirq, cirq_google
import qsimcirq
def sim(width: int, height: int, reps: int, use_gpu: bool, gpu_mode: int):
rqc_fn = cirq.experiments.random_rotations_between_grid_interaction_layers_circuit
qvm_fn = cirq_google.engine.create_default_noisy_quantum_virtual_machine
qubits = cirq.GridQubit.rect(width, height, 3, 2)
circuit = rqc_fn(qubits, depth=10, seed=0) + cirq.measure(*qubits, key="final_state")
processor_id = "weber"
qsim_options = qsimcirq.QSimOptions(use_gpu=use_gpu, gpu_mode=gpu_mode)
# we do not recommend using seed=0 in production usage; in this
# example it helps compare performance between runs
qvm = qvm_fn(processor_id, qsimcirq.QSimSimulator, seed=0, qsim_options=qsim_options)
start = time.time()
results = qvm.get_sampler(processor_id).run(circuit, repetitions=reps)
print(results)
print(f"elapsed: {time.time() - start:.03f}s")
if __name__ == "__main__":
width, height, reps = 5, 5, 10
print("This series of simulations should last approximately 1 minute on an A2 series VM\n")
print("Running on CPU:")
sys.stdout.flush()
sim(width=width, height=height, reps=reps, use_gpu=False, gpu_mode=0)
print("\nRunning on GPU (CUDA):")
sys.stdout.flush()
sim(width=width, height=height, reps=reps, use_gpu=True, gpu_mode=0)
print("\nRunning on GPU (CUDA + cuQuantum):")
sys.stdout.flush()
sim(width=width, height=height, reps=reps, use_gpu=True, gpu_mode=1)
- type: shell
destination: run-qsim.sh
content: |
#!/bin/bash -i
# The -i above (for interactive) is required so that conda command will be accessible.
# this script demonstrates how to run the qsim example application and
# also "warms up" the GPU to give reliable performance metrics
conda activate qsim
python /var/tmp/qsim-example.py
- id: qsimvm
source: modules/compute/vm-instance
use:
- network1
- quantum-simulator-setup
settings:
machine_type: n1-standard-32
guest_accelerator:
- type: nvidia-tesla-t4
count: 1
instance_image:
project: ubuntu-os-cloud
family: ubuntu-2004-lts
- id: wait
source: community/modules/scripts/wait-for-startup
settings:
instance_name: ((module.qsimvm.name[0]))
timeout: 2400