Skip to content

Commit

Permalink
Merge branch 'v23.10' into 'main'
Browse files Browse the repository at this point in the history
Update for 23.10 release

See merge request cuda-hpc-libraries/cuquantum-sdk/cuquantum-public!25
  • Loading branch information
mtjrider committed Nov 3, 2023
2 parents c202ee7 + 61bc3fa commit 2ac5645
Show file tree
Hide file tree
Showing 82 changed files with 6,884 additions and 830 deletions.
2 changes: 1 addition & 1 deletion benchmarks/cuquantum_benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#
# SPDX-License-Identifier: BSD-3-Clause

__version__ = '0.3.0'
__version__ = '0.3.1'
2 changes: 1 addition & 1 deletion benchmarks/cuquantum_benchmarks/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def get_cpu_name():
if m:
return m.group(0).split(':')[-1].strip()
else:
assert False, f"getting cpu info failed"
return f"unknown"


def get_gpu_driver_version():
Expand Down
10 changes: 1 addition & 9 deletions benchmarks/cuquantum_benchmarks/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

from .backend_cirq import Cirq
from .backend_cutn import cuTensorNet
from .backend_pny import (Pny, PnyLightningGpu, PnyLightningCpu,
PnyLightningKokkos, PnyDumper)
from .backend_pny import Pny, PnyLightningGpu, PnyLightningCpu, PnyLightningKokkos
from .backend_qsim import Qsim, QsimCuda, QsimCusv, QsimMgpu
from .backend_qiskit import Aer, AerCuda, AerCusv, CusvAer
from .backend_qulacs import QulacsGpu, QulacsCpu
try:
from .backend_naive import Naive
except ImportError:
Naive = None


backends = {
Expand All @@ -30,12 +25,9 @@
'pennylane-lightning-gpu': PnyLightningGpu,
'pennylane-lightning-qubit': PnyLightningCpu,
'pennylane-lightning-kokkos': PnyLightningKokkos,
'pennylane-dumper': PnyDumper,
'qulacs-cpu': QulacsCpu,
'qulacs-gpu': QulacsGpu,
}
if Naive:
backends['naive'] = Naive


def createBackend(backend_name, ngpus, ncpu_threads, precision, *args, **kwargs):
Expand Down
24 changes: 20 additions & 4 deletions benchmarks/cuquantum_benchmarks/backends/backend_cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
#
# SPDX-License-Identifier: BSD-3-Clause

import functools
import warnings

try:
import cirq
except ImportError:
cirq = None


try:
from .. import _internal_utils
except ImportError:
_internal_utils = None
from .backend import Backend


class Cirq(Backend):
class _Cirq(Backend):

def __init__(self, ngpus, ncpu_threads, precision, *args, **kwargs):
def __init__(self, ngpus, ncpu_threads, precision, *args, identifier=None, **kwargs):
if cirq is None:
raise RuntimeError("cirq is not installed")
if ngpus > 0:
Expand All @@ -25,7 +30,15 @@ def __init__(self, ngpus, ncpu_threads, precision, *args, **kwargs):
raise ValueError("the cirq backend only supports single precision")

self.backend = cirq.Simulator()

self.identifier = identifier
self.version = cirq.__version__

def preprocess_circuit(self, circuit, *args, **kwargs):
if _internal_utils is not None:
_internal_utils.preprocess_circuit(self.identifier, circuit, *args, **kwargs)

return {}

def run(self, circuit, nshots=1024):
run_data = {}
if nshots > 0:
Expand All @@ -34,3 +47,6 @@ def run(self, circuit, nshots=1024):
results = self.backend.simulate(circuit)
post_res = results.measurements['result']
return {'results': results, 'post_results': post_res, 'run_data': run_data}


Cirq = functools.partial(_Cirq, identifier='cirq')
1 change: 1 addition & 0 deletions benchmarks/cuquantum_benchmarks/backends/backend_cutn.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, ngpus, ncpu_threads, precision, **kwargs):
opts = cutn.NetworkOptions(handle=self.handle)
self.network_opts = opts
self.n_samples = kwargs.pop('nhypersamples')
self.version = cutn.get_version()

def __del__(self):
cutn.destroy(self.handle)
Expand Down
57 changes: 30 additions & 27 deletions benchmarks/cuquantum_benchmarks/backends/backend_pny.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
import os
import time
import warnings
import sys

import numpy as np
try:
import pennylane
except ImportError:
pennylane = None

try:
from .. import _internal_utils
except ImportError:
_internal_utils = None
from .backend import Backend
from .._utils import call_by_root, EarlyReturnError, is_running_mpi


# set up a logger
Expand All @@ -35,16 +37,36 @@ def __init__(self, ngpus, ncpu_threads, precision, *args, identifier=None, **kwa
self.ncpu_threads = ncpu_threads
self.nqubits = kwargs.pop('nqubits')
self.circuit = None
self.version = self.find_version(identifier)

def _make_qnode(self, circuit, nshots=1024, **kwargs):
if self.identifier == "pennylane-lightning-gpu":
def find_version(self, identifier):
if identifier == "pennylane-lightning-gpu":
if self.ngpus == 1:
try:
import pennylane_lightning_gpu
except ImportError as e:
raise RuntimeError("PennyLane-Lightning-GPU plugin is not installed") from e
else:
raise ValueError(f"cannot specify --ngpus > 1 for the backend {self.identifier}")
raise ValueError(f"cannot specify --ngpus > 1 for the backend {identifier}")
ver = pennylane_lightning_gpu.__version__
elif identifier == "pennylane-lightning-kokkos":
try:
import pennylane_lightning_kokkos
except ImportError as e:
raise RuntimeError("PennyLane-Lightning-Kokkos plugin is not installed") from e
ver = pennylane_lightning_kokkos.__version__
elif identifier == "pennylane-lightning-qubit":
try:
from pennylane_lightning import lightning_qubit
except ImportError as e:
raise RuntimeError("PennyLane-Lightning plugin is not installed") from e
ver = lightning_qubit.__version__
else: # identifier == "pennylane"
ver = pennylane.__version__
return ver

def _make_qnode(self, circuit, nshots=1024, **kwargs):
if self.identifier == "pennylane-lightning-gpu":
dev = pennylane.device("lightning.gpu", wires=self.nqubits, shots=nshots, c_dtype=self.dtype)
elif self.identifier == "pennylane-lightning-kokkos":
# there's no way for us to query what execution space (=backend) that kokkos supports at runtime,
Expand All @@ -67,10 +89,6 @@ def _make_qnode(self, circuit, nshots=1024, **kwargs):
sync=False,
kokkos_args=args)
elif self.identifier == "pennylane-lightning-qubit":
try:
import pennylane_lightning
except ImportError as e:
raise RuntimeError("PennyLane-Lightning plugin is not installed") from e
if self.ngpus != 0:
raise ValueError(f"cannot specify --ngpus for the backend {self.identifier}")
if self.ncpu_threads > 1 and self.ncpu_threads != int(os.environ.get("OMP_NUM_THREADS", "-1")):
Expand All @@ -81,30 +99,16 @@ def _make_qnode(self, circuit, nshots=1024, **kwargs):
if self.ngpus != 0:
raise ValueError(f"cannot specify --ngpus for the backend {self.identifier}")
dev = pennylane.device("default.qubit", wires=self.nqubits, shots=nshots, c_dtype=self.dtype)
elif self.identifier == "pennylane-dumper":
import cloudpickle
import cuquantum_benchmarks
cloudpickle.register_pickle_by_value(cuquantum_benchmarks)

# note: before loading the pickle, one should check if the Python version agrees
# (probably pennylane's version too)
py_major_minor = f'{sys.version_info.major}.{sys.version_info.minor}'
circuit_filename = kwargs.pop('circuit_filename')
circuit_filename += f"_pny_raw_py{py_major_minor}.pickle"
def dump():
logger.info(f"dumping pennylane (raw) circuit as {circuit_filename} ...")
with open(circuit_filename, 'wb') as f:
cloudpickle.dump(circuit, f) # use highest protocol
logger.info("early exiting as the dumper task is completed")
call_by_root(dump)
raise EarlyReturnError
else:
raise ValueError(f"the backend {self.identifier} is not recognized")

qnode = pennylane.QNode(circuit, device=dev)
return qnode

def preprocess_circuit(self, circuit, *args, **kwargs):
if _internal_utils is not None:
_internal_utils.preprocess_circuit(self.identifier, circuit, *args, **kwargs)

nshots = kwargs.get('nshots', 1024)
t1 = time.perf_counter()
self.circuit = self._make_qnode(circuit, nshots, **kwargs)
Expand All @@ -125,4 +129,3 @@ def run(self, circuit, nshots=1024):
PnyLightningCpu = functools.partial(Pennylane, identifier='pennylane-lightning-qubit')
PnyLightningKokkos = functools.partial(Pennylane, identifier='pennylane-lightning-kokkos')
Pny = functools.partial(Pennylane, identifier='pennylane')
PnyDumper = functools.partial(Pennylane, identifier='pennylane-dumper')
19 changes: 16 additions & 3 deletions benchmarks/cuquantum_benchmarks/backends/backend_qiskit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
except ImportError:
qiskit = None

try:
from .. import _internal_utils
except ImportError:
_internal_utils = None
from .backend import Backend
from .._utils import get_mpi_size, get_mpi_rank
from .._utils import call_by_root, EarlyReturnError


# set up a logger
Expand All @@ -32,9 +37,18 @@ def __init__(self, ngpus, ncpu_threads, precision, *args, identifier=None, **kwa
self.precision = precision
self.identifier = identifier
self.nqubits = kwargs.pop('nqubits')
self.backend = self.create_aer_backend(identifier, ngpus, ncpu_threads, *args, **kwargs)
self.version = self.find_version(identifier)
self.backend = self.create_aer_backend(self.identifier, ngpus, ncpu_threads, *args, **kwargs)

def find_version(self, identifier):
if identifier == 'cusvaer':
return version('cusvaer')
return qiskit.__qiskit_version__['qiskit-aer']

def preprocess_circuit(self, circuit, *args, **kwargs):
if _internal_utils is not None:
_internal_utils.preprocess_circuit(self.identifier, circuit, *args, **kwargs)

t0 = time.perf_counter()
self.transpiled_qc = qiskit.transpile(circuit, self.backend) # (circuit, basis_gates=['u3', 'cx'], backend=self.backend)
t1 = time.perf_counter()
Expand Down Expand Up @@ -165,8 +179,7 @@ def create_aer_backend(self, identifier, ngpus, ncpu_threads, *args, **kwargs):
blocking_enable=blocking_enable, blocking_qubits=blocking_qubits,
fusion_max_qubit=nfused, precision=self.precision)
else:
raise ValueError(f"the backend {identifier} is not recognized")

backend = None
return backend

def get_aer_blocking_setup(self, ngpus=None):
Expand Down
1 change: 1 addition & 0 deletions benchmarks/cuquantum_benchmarks/backends/backend_qsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, ngpus, ncpu_threads, precision, *args, identifier=None, **kwa
raise ValueError("all qsim backends only support single precision")
self.identifier = identifier
qsim_options = self.create_qsim_options(identifier, ngpus, ncpu_threads, **kwargs)
self.version = qsimcirq.__version__
self.backend = qsimcirq.QSimSimulator(qsim_options=qsim_options)

def run(self, circuit, nshots=1024):
Expand Down
1 change: 1 addition & 0 deletions benchmarks/cuquantum_benchmarks/backends/backend_qulacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, ngpus, ncpu_threads, precision, *args, identifier=None, **kwa
self.ncpu_threads = ncpu_threads
self.nqubits = kwargs.pop('nqubits')
self.state = self.create_qulacs_state()
self.version = qulacs.__version__

def create_qulacs_state(self):
if self.identifier == 'qulacs-gpu':
Expand Down
21 changes: 0 additions & 21 deletions benchmarks/cuquantum_benchmarks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,6 @@
},
},

'naive': {
'config': {
'nshots': 1024,
'nfused': None,
'ngpus': 1,
'ncputhreads': 0,
'precision': 'single',
},
},

'pennylane': {
'config': {
'nshots': 1024,
Expand Down Expand Up @@ -245,17 +235,6 @@
},
},

# dummy
'pennylane-dumper': {
'config': {
'nshots': 1024,
'nfused': None,
'ngpus': 0,
'ncputhreads': 1,
'precision': 'single',
},
},

'qulacs-gpu': {
'config': {
'nshots': 1024,
Expand Down
7 changes: 1 addition & 6 deletions benchmarks/cuquantum_benchmarks/frontends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
from .frontend_qiskit import Qiskit
from .frontend_pny import Pennylane
from .frontend_qulacs import Qulacs
try:
from .frontend_naive import Naive
except ImportError:
Naive = None


frontends = {
Expand All @@ -18,8 +14,7 @@
'pennylane': Pennylane,
'qulacs': Qulacs
}
if Naive:
frontends['naive'] = Naive


def createFrontend(frontend_name, nqubits, config):
return frontends[frontend_name](nqubits, config)
6 changes: 3 additions & 3 deletions benchmarks/cuquantum_benchmarks/frontends/frontend_pny.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, nqubits, config):
def generateCircuit(self, gateSeq):
last_g = gateSeq[-1]
assert last_g.id == "measure" # TODO: relax this?

def circuit():
measured_qs = None

Expand Down Expand Up @@ -71,5 +71,5 @@ def circuit():
raise NotImplementedError(f"The gate type {g.id} is not defined")

return pennylane.sample(wires=measured_qs)

return circuit
return circuit
12 changes: 10 additions & 2 deletions benchmarks/cuquantum_benchmarks/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
from .run_interface import BenchApiRunner, BenchCircuitRunner
from ._utils import (EarlyReturnError, MPHandler, RawTextAndDefaultArgFormatter,
str_to_seq,)
try:
from . import _internal_utils
except ImportError:
_internal_utils = None


if _internal_utils is not None:
_internal_utils.init()


frontend_names = [f for f in frontends.keys()]
Expand Down Expand Up @@ -300,8 +308,8 @@ def run(args=None):
del args.benchmark
config = backend_config[args.backend]

if ((args.frontend == 'cirq' and args.backend not in ('cirq', 'cutn', *[k for k in backends.keys() if k.startswith('qsim')]))
or (args.frontend == 'qiskit' and args.backend not in ('cutn', *[k for k in backends.keys() if 'aer' in k]))
if ((args.frontend == 'cirq' and args.backend not in (*[k for k in backends.keys() if k.startswith('cirq')], 'cutn', *[k for k in backends.keys() if k.startswith('qsim')]))
or (args.frontend == 'qiskit' and args.backend not in ('cutn', *[k for k in backends.keys() if k.startswith('qiskit')], *[k for k in backends.keys() if 'aer' in k]))
or (args.frontend == 'naive' and args.backend != 'naive')
or (args.frontend == 'pennylane' and not args.backend.startswith('pennylane'))
or (args.frontend == 'qulacs' and not args.backend.startswith('qulacs'))):
Expand Down
Loading

0 comments on commit 2ac5645

Please sign in to comment.