forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow the environment variable to override the default target (NVIDIA…
…#877) * * Allow the environment variable to override the default target -- Added an environment variable - CUDAQ_DEFAULT_SIMULATOR -- Check value against valid simulators -- Added basic tests * Update tools/nvqpp/nvq++.in Co-authored-by: Eric Schweitz <[email protected]> * * Check environment variable and GPU availability in the LinkedLibraryHolder constructor. * * Clean up code from Python cudaq module (moved to LinkedLibraryHolder) * Update python/utils/LinkedLibraryHolder.cpp Co-authored-by: Ben Howe <[email protected]> * * Addressing review comments - enhanced tests * * Explicitly set simulator along with target * * Addressing review comments - Python tests modified for clarity * Initializing current target to default in Python * Update python/utils/LinkedLibraryHolder.cpp Co-authored-by: Eric Schweitz <[email protected]> * Update python/utils/LinkedLibraryHolder.h Co-authored-by: Eric Schweitz <[email protected]> * Update python/utils/LinkedLibraryHolder.h Co-authored-by: Eric Schweitz <[email protected]> * * Removing 'NULL' check --------- Co-authored-by: Eric Schweitz <[email protected]> Co-authored-by: Ben Howe <[email protected]>
- Loading branch information
1 parent
0ccf994
commit df34f7f
Showing
8 changed files
with
231 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# ============================================================================ # | ||
# Copyright (c) 2022 - 2023 NVIDIA Corporation & Affiliates. # | ||
# All rights reserved. # | ||
# # | ||
# This source code and the accompanying materials are made available under # | ||
# the terms of the Apache License 2.0 which accompanies this distribution. # | ||
# ============================================================================ # | ||
|
||
# RUN: PYTHONPATH=../../ pytest -rP %s | ||
|
||
import os | ||
os.environ["CUDAQ_DEFAULT_SIMULATOR"] = "density-matrix-cpu" | ||
|
||
import pytest | ||
|
||
import cudaq | ||
|
||
def test_default_target(): | ||
"""Tests the default target set by environment variable""" | ||
|
||
assert ("density-matrix-cpu" == cudaq.get_target().name) | ||
|
||
kernel = cudaq.make_kernel() | ||
qubits = kernel.qalloc(2) | ||
kernel.h(qubits[0]) | ||
kernel.cx(qubits[0], qubits[1]) | ||
kernel.mz(qubits) | ||
|
||
result = cudaq.sample(kernel) | ||
result.dump() | ||
assert '00' in result | ||
assert '11' in result | ||
|
||
def test_env_var_with_emulate(): | ||
"""Tests the target when emulating a hardware backend""" | ||
|
||
assert ("density-matrix-cpu" == cudaq.get_target().name) | ||
cudaq.set_target("quantinuum", emulate=True) | ||
assert ("quantinuum" == cudaq.get_target().name) | ||
|
||
kernel = cudaq.make_kernel() | ||
qubits = kernel.qalloc(2) | ||
kernel.h(qubits[0]) | ||
kernel.cx(qubits[0], qubits[1]) | ||
kernel.mz(qubits) | ||
|
||
result = cudaq.sample(kernel) | ||
result.dump() | ||
assert '00' in result | ||
assert '11' in result | ||
|
||
def test_target_override(): | ||
"""Tests the target set by environment variable is overridden by user setting""" | ||
|
||
cudaq.set_target("qpp-cpu") | ||
assert("qpp-cpu" == cudaq.get_target().name) | ||
|
||
kernel = cudaq.make_kernel() | ||
qubits = kernel.qalloc(2) | ||
kernel.h(qubits[0]) | ||
kernel.cx(qubits[0], qubits[1]) | ||
kernel.mz(qubits) | ||
|
||
result = cudaq.sample(kernel) | ||
result.dump() | ||
assert '00' in result | ||
assert '11' in result | ||
|
||
os.environ.pop("CUDAQ_DEFAULT_SIMULATOR") | ||
|
||
# leave for gdb debugging | ||
if __name__ == "__main__": | ||
loc = os.path.abspath(__file__) | ||
pytest.main([loc, "-rP"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ============================================================================ # | ||
# Copyright (c) 2022 - 2023 NVIDIA Corporation & Affiliates. # | ||
# All rights reserved. # | ||
# # | ||
# This source code and the accompanying materials are made available under # | ||
# the terms of the Apache License 2.0 which accompanies this distribution. # | ||
# ============================================================================ # | ||
|
||
# RUN: PYTHONPATH=../../ pytest -rP %s | ||
|
||
import os | ||
os.environ["CUDAQ_DEFAULT_SIMULATOR"] = "density-matrix-cpu" | ||
|
||
import pytest | ||
|
||
import cudaq | ||
|
||
def test_env_var_update(): | ||
"""Tests that if the environment variable does not take effect on-the-fly""" | ||
|
||
os.environ["CUDAQ_DEFAULT_SIMULATOR"] = "qpp-cpu" | ||
assert("qpp-cpu" != cudaq.get_target().name) | ||
|
||
cudaq.set_target("qpp-cpu") | ||
assert("qpp-cpu" == cudaq.get_target().name) | ||
|
||
kernel = cudaq.make_kernel() | ||
qubits = kernel.qalloc(2) | ||
kernel.h(qubits[0]) | ||
kernel.cx(qubits[0], qubits[1]) | ||
kernel.mz(qubits) | ||
|
||
result = cudaq.sample(kernel) | ||
result.dump() | ||
assert '00' in result | ||
assert '11' in result | ||
|
||
cudaq.reset_target() | ||
assert ("density-matrix-cpu" == cudaq.get_target().name) | ||
|
||
os.environ.pop("CUDAQ_DEFAULT_SIMULATOR") | ||
|
||
# leave for gdb debugging | ||
if __name__ == "__main__": | ||
loc = os.path.abspath(__file__) | ||
pytest.main([loc, "-rP"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters