-
Notifications
You must be signed in to change notification settings - Fork 54
Fix mixer forwarding | Fix MPI implementation | Add test cases #452
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
672c80c
Fix mixer forwarding | Fix MPI implementation | Add test cases
kaiqiy-nv ebb568f
Merge branch 'main' into kaiqiy/bug-fix-combined
kaiqiy-nv 000960a
Merge branch 'main' into kaiqiy/bug-fix-combined
bmhowe23 d4f1930
Merge branch 'main' into kaiqiy/bug-fix-combined
bmhowe23 0b341d9
Reform MPI C++/Python tests
kaiqiy-nv c283051
Reform MPI C++/Python tests
kaiqiy-nv 48af886
Merge branch 'main' into kaiqiy/bug-fix-combined
kaiqiy-nv fba7cdc
Use ob1 MPI on CPU-only runners
kaiqiy-nv 3029dcc
Reform MPI C++/Python tests
kaiqiy-nv 52fd669
Update python tests
kaiqiy-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,90 @@ | ||
| # ============================================================================ # | ||
| # Copyright (c) 2024 - 2026 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. # | ||
| # ============================================================================ # | ||
|
|
||
| # ADAPT-VQE MPI work-splitting test. | ||
| # Verifies that ADAPT-VQE produces the correct H2 ground-state energy when | ||
| # commutator evaluation is distributed across multiple MPI ranks. | ||
| # | ||
| # When invoked by pytest, the test launches itself via mpiexec as a subprocess. | ||
| # When invoked by mpiexec, the __name__ == "__main__" block runs the actual | ||
| # MPI computation. | ||
|
|
||
| import os | ||
| import shutil | ||
| import subprocess | ||
|
|
||
| import pytest | ||
|
|
||
| _MPI_CMD = ["mpiexec", "--allow-run-as-root", "--oversubscribe"] | ||
|
|
||
|
|
||
| def _mpi_available(): | ||
| if shutil.which("mpiexec") is None: | ||
| return False | ||
| # Probe with 2 ranks to verify multi-rank MPI actually works | ||
| # (catches missing PML transports, absent cudaq MPI plugin, etc.) | ||
| try: | ||
| rc = subprocess.run(_MPI_CMD + [ | ||
| "-np", "2", "python3", "-c", | ||
| "import cudaq; cudaq.mpi.initialize(); " | ||
| "cudaq.mpi.finalize()" | ||
| ], | ||
| capture_output=True, | ||
| timeout=30) | ||
| return rc.returncode == 0 | ||
| except subprocess.TimeoutExpired: | ||
| return False | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not _mpi_available(), | ||
| reason="mpiexec or cudaq MPI support not available") | ||
| @pytest.mark.parametrize("num_ranks", [2, 4]) | ||
| def test_adapt_mpi(num_ranks): | ||
| result = subprocess.run(_MPI_CMD + | ||
| ["-np", str(num_ranks), "python3", __file__], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=120) | ||
| assert result.returncode == 0, \ | ||
| (f"MPI test failed (np={num_ranks}):\n" | ||
| f"--- stdout ---\n{result.stdout}\n" | ||
| f"--- stderr ---\n{result.stderr}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import numpy as np | ||
| import cudaq | ||
| import cudaq_solvers as solvers | ||
|
|
||
| cudaq.mpi.initialize() | ||
|
|
||
| geometry = [('H', (0., 0., 0.)), ('H', (0., 0., .7474))] | ||
| molecule = solvers.create_molecule(geometry, 'sto-3g', 0, 0, casci=True) | ||
| operators = solvers.get_operator_pool("spin_complement_gsd", | ||
| num_orbitals=molecule.n_orbitals) | ||
| numElectrons = molecule.n_electrons | ||
|
|
||
| @cudaq.kernel | ||
| def initState(q: cudaq.qview): | ||
| for i in range(numElectrons): | ||
| x(q[i]) | ||
|
|
||
| energy, thetas, ops = solvers.adapt_vqe(initState, molecule.hamiltonian, | ||
| operators) | ||
|
|
||
| rank = cudaq.mpi.rank() | ||
| num_ranks = cudaq.mpi.num_ranks() | ||
|
|
||
| if rank == 0: | ||
| print(f"[MPI ADAPT] ranks={num_ranks}, energy={energy:.6f}") | ||
| assert np.isclose(energy, -1.137, atol=1e-3), \ | ||
| f"MPI ADAPT energy {energy} does not match expected -1.137" | ||
| assert len(ops) > 0, "Expected at least one operator to be selected" | ||
| print("PASS") | ||
|
|
||
| cudaq.mpi.finalize() |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.