Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance enhancements: observe_n and sample_n broadcast functions #74

Merged
merged 15 commits into from
May 8, 2023

Conversation

amccaskey
Copy link
Collaborator

@amccaskey amccaskey commented Apr 6, 2023

#62

What's changing

  • Use custatevecApplyPauliRotation for rx,ry,rz (faster than just custatevecApplyMatrix)
  • CircuitSimulator batch mode (keep state memory alive during batch execution, faster than allocation, operation, deallocation over and over again)
  • Simulators compute expectation value via in-memory matrix-vector product and dot product (<psi | H | psi>, faster for lower qubit counts than sampling the state).
  • observe_n and sample_n batch mode execution (faster than iterating over many observe / sample calls)
  • observe_n and sample_n distribute work to all available QPUs on the platform.

Runtime Improvements Example

For the following code,

n_samples = 5000
h = spin.z(0) 
times = []
for n_qubits in range(2, 16, 1): 
    n_parameters = n_qubits*3
    parameters = np.random.default_rng(13).uniform(low=0, high=1, size = (n_samples,n_parameters))
    np.random.seed(1)

    kernel, params = cudaq.make_kernel(list)
    qubits = kernel.qalloc(n_qubits)
    qubits_list = list(range(n_qubits))

    for i in range(n_qubits):
        kernel.rx(params[i], qubits[i])
    for i in range(n_qubits):
        kernel.ry(params[i + n_qubits], qubits[i])
    for i in range(n_qubits):
        kernel.rz(params[i + n_qubits*2], qubits[i])
    for q1, q2 in zip(qubits_list[0::2], qubits_list[1::2]):
        kernel.cz(qubits[q1], qubits[q2])

    t = []
    for _ in range(1): 
        start = timeit.default_timer()
        exp_vals = cudaq.observe_n(kernel, h, parameters)
        # old way, in main branch now
        # exp_vals = [cudaq.observe(kernel, h, parameters[i]).expectation_z() for i in range(parameters.shape[0])]
        end = timeit.default_timer()
        assert len(exp_vals) == n_samples
        t.append(end-start)
    
    times.append(statistics.mean(t))

we get the following runtime plots
perfEnhancement
perfEnhancement

@bettinaheim bettinaheim added the release notes Changes need to be captured in the release notes label Apr 11, 2023
@amccaskey amccaskey changed the title Implement observe_n and sample_n broadcast functions in C++ and Python Performance enhancements: observe_n and sample_n broadcast functions Apr 11, 2023
@amccaskey amccaskey marked this pull request as ready for review April 11, 2023 19:15
@amccaskey amccaskey force-pushed the observe_n branch 3 times, most recently from d7058e1 to 69722e2 Compare April 18, 2023 23:57
@amccaskey amccaskey force-pushed the observe_n branch 3 times, most recently from 9dbce5e to bebf005 Compare April 26, 2023 12:06
Copy link
Collaborator

@boschmitt boschmitt left a comment

Choose a reason for hiding this comment

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

Overall LGTM, thanks @amccaskey.

I would still want @anthony-santana, which has more familiarity with the python API, to look over.

python/utils/OpaqueArguments.h Outdated Show resolved Hide resolved
runtime/nvqir/qpp/QppCircuitSimulator.cpp Outdated Show resolved Hide resolved
runtime/nvqir/qpp/QppCircuitSimulator.cpp Outdated Show resolved Hide resolved
runtime/nvqir/CircuitSimulator.h Outdated Show resolved Hide resolved
runtime/nvqir/custatevec/CuStateVecCircuitSimulator.cu Outdated Show resolved Hide resolved
runtime/nvqir/CircuitSimulator.h Outdated Show resolved Hide resolved
runtime/nvqir/CircuitSimulator.h Outdated Show resolved Hide resolved
@amccaskey amccaskey force-pushed the observe_n branch 2 times, most recently from acd36b9 to 0055389 Compare May 1, 2023 14:17
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
Signed-off-by: Alex McCaskey <amccaskey@nvidia.com>
@amccaskey amccaskey merged commit b431b93 into NVIDIA:main May 8, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 8, 2023
@bettinaheim bettinaheim added the enhancement New feature or request label Jun 28, 2023
@amccaskey amccaskey deleted the observe_n branch September 13, 2023 23:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request release notes Changes need to be captured in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants