KEMBAR78
Optimize kernel mode for hybrid QNN test case by bmhowe23 · Pull Request #1572 · NVIDIA/cuda-quantum · GitHub
Skip to content

Conversation

@bmhowe23
Copy link
Collaborator

@bmhowe23 bmhowe23 commented Apr 25, 2024

This PR optimizes for potential future modifications to examples/python/tutorials/hybrid_qnns.ipynb.

Enumerating and looping over all elements in inspect.stack() was incredibly slow when running in a Jupyter notebook. (Interestingly enough, the slowness was not apparent when running a Python script with the python3 command.)

Here are some sample timings before the change:

# Kernel mode
qubit_count = 1
@cudaq.kernel
def kernel():
    qubits = cudaq.qvector(qubit_count)
cudaq.sample(kernel)
%timeit cudaq.sample(kernel)
# 4.73 ms ± 156 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

# Builder mode
qubit_count = 1
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(qubit_count)
cudaq.sample(kernel)
%timeit cudaq.sample(kernel)
# 220 µs ± 26 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

And after the change:

# Kernel mode
qubit_count = 1
@cudaq.kernel
def kernel():
    qubits = cudaq.qvector(qubit_count)
cudaq.sample(kernel)
%timeit cudaq.sample(kernel)
# 209 µs ± 6.2 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each) <------- large reduction compared to 4.73 ms

# Builder mode
qubit_count = 1
kernel = cudaq.make_kernel()
qubits = kernel.qalloc(qubit_count)
cudaq.sample(kernel)
%timeit cudaq.sample(kernel)
# 200 µs ± 16.3 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

This is a >20x speedup for this test case, which brings the kernel mode in line with the builder mode.

@bmhowe23 bmhowe23 changed the title Optimize kernel mode for Hybrid QNN test case Optimize kernel mode for hybrid QNN test case Apr 26, 2024
@github-actions
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Apr 26, 2024
@bmhowe23 bmhowe23 force-pushed the pr-optimize-hybrid-qnn branch from 88f8ccf to b90b2dc Compare April 26, 2024 02:17
@github-actions
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Apr 26, 2024
@bmhowe23 bmhowe23 marked this pull request as ready for review April 26, 2024 12:27
@bmhowe23 bmhowe23 requested a review from amccaskey April 26, 2024 12:30
Copy link
Collaborator

@amccaskey amccaskey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@github-actions
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Apr 26, 2024
@bmhowe23 bmhowe23 merged commit 00253a8 into NVIDIA:main Apr 26, 2024
@bmhowe23 bmhowe23 deleted the pr-optimize-hybrid-qnn branch April 26, 2024 16:07
@github-actions github-actions bot locked and limited conversation to collaborators Apr 26, 2024
@bettinaheim bettinaheim added this to the release 0.7.1 milestone Apr 30, 2024
@bmhowe23 bmhowe23 added the enhancement New feature or request label May 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants