Skip to content

Port mgpu gqe - #201

Merged
melody-ren merged 56 commits into
NVIDIA:mainfrom
melody-ren:port_mgpu_gqe
Jul 22, 2025
Merged

melody-ren merged 56 commits into
NVIDIA:mainfrom
melody-ren:port_mgpu_gqe

Conversation

@melody-ren

Copy link
Copy Markdown
Collaborator

No description provided.

Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 2, 2025

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 6c4f212

Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 0142ed6

@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 55275a7

Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 577b6af

@melody-ren
melody-ren requested a review from marwafar July 7, 2025 23:33
@marwafar

marwafar commented Jul 8, 2025 •

Copy link
Copy Markdown
Collaborator

Where is the log file of the gqe is saved? User might need to check the training performance. Currently, loss value is not provided. Therefore, it is hard to know if the model converged or not.

@marwafar

marwafar commented Jul 8, 2025

Copy link
Copy Markdown
Collaborator

I tried this example below which represents the H2 molecule. Each time I run it, it gives different results. This was not happening with old version. Output was -1.137.

import numpy as np
import cudaq, cudaq_solvers as solvers
from cudaq import spin


n_qubits = 4
n_electrons = 2

spin_ham = - 0.106477 + 0.17028 * spin.z(0) + 0.17028 * spin.z(1) - 0.220041 * spin.z(2) 
spin_ham+= -0.220041 * spin.z(3) + 0.1202 * spin.z(0) * spin.z(2) 
spin_ham+= 0.168336 * spin.z(0) * spin.z(1)  
spin_ham+= 0.0454063 * spin.y(0) * spin.x(1) * spin.x(2) * spin.y(3)  
spin_ham-=0.0454063 * spin.y(0) * spin.y(1) * spin.x(2) * spin.x(3) 
spin_ham-=0.0454063 * spin.x(0) * spin.x(1) * spin.y(2) * spin.y(3) 
spin_ham+=0.0454063 * spin.x(0) * spin.y(1) * spin.y(2) * spin.x(3) 
spin_ham+=0.165607 * spin.z(0) * spin.z(3) + 0.1202 * spin.z(1) * spin.z(3)  
spin_ham+=0.165607 * spin.z(1) * spin.z(2) + 0.174073 * spin.z(2) * spin.z(3)
    

params = [0.003125, -0.003125, 0.00625,
            -0.00625, 0.0125, -0.0125, 0.025,
            -0.025, 0.05, -0.05, 0.1, -0.1]

def pool(params):
    ops = []
    i=0
      
    ops.append(cudaq.SpinOperator(spin.y(i)*spin.z(i+1)*spin.x(i+2)*spin.i(i+3)))
    ops.append(cudaq.SpinOperator(spin.x(i)*spin.z(i+1)*spin.y(i+2)*spin.i(i+3)))
    ops.append(cudaq.SpinOperator(spin.i(i)*spin.y(i+1)*spin.z(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.i(i)*spin.x(i+1)*spin.z(i+2)*spin.y(i+3)))
    ops.append(cudaq.SpinOperator(spin.x(i)*spin.x(i+1)*spin.x(i+2)*spin.y(i+3)))
    ops.append(cudaq.SpinOperator(spin.x(i)*spin.x(i+1)*spin.y(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.x(i)*spin.y(i+1)*spin.y(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.y(i)*spin.x(i+1)*spin.y(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.x(i)*spin.y(i+1)*spin.x(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.y(i)*spin.x(i+1)*spin.x(i+2)*spin.x(i+3)))
    ops.append(cudaq.SpinOperator(spin.y(i)*spin.y(i+1)*spin.x(i+2)*spin.y(i+3)))
    ops.append(cudaq.SpinOperator(spin.y(i)*spin.y(i+1)*spin.y(i+2)*spin.x(i+3)))
    
    pool =[]
    for c in params:
        for op in ops:
            pool.append(c * op)
    
    return pool
        
op_pool = pool(params)

def term_coefficients(op: cudaq.SpinOperator) -> list[complex]:
    return [term.evaluate_coefficient() for term in op]


def term_words(op: cudaq.SpinOperator) -> list[cudaq.pauli_word]:
    return [term.get_pauli_word(n_qubits) for term in op]


# Kernel that applies the selected operators
@cudaq.kernel
def kernel(n_qubits: int, n_electrons: int, coeffs: list[float], words: list[cudaq.pauli_word]):
    q = cudaq.qvector(n_qubits)
    
    for i in range(n_electrons):
        x(q[i])
    
    for i in range(len(coeffs)):
        exp_pauli(coeffs[i], q, words[i])

def cost(sampled_ops: list[cudaq.SpinOperator], **kwargs):
    
    full_coeffs = []
    full_words = []
    
    for op in sampled_ops:
        full_coeffs += [c.real for c in term_coefficients(op)]
        full_words += term_words(op)

    return cudaq.observe(kernel, spin_ham, n_qubits, n_electrons, full_coeffs,
                         full_words).expectation()

minE, best_ops = solvers.gqe(cost, op_pool, max_iters=25, ngates=20)

print(f'Ground Energy = {minE}')
print('Ansatz Ops')
for idx in best_ops:
    # Get the first (and only) term since these are simple operators
    term = next(iter(op_pool[idx]))
    print(term.evaluate_coefficient().real,
            term.get_pauli_word(n_qubits))

@melody-ren

Copy link
Copy Markdown
Collaborator Author

Where is the log file of the gqe is saved? User might need to check the training performance. Currently, even loss value is not provided. Therefore, it is hard to know if the model converged or not.

I'll look into this and also add logging for the loss value.

@konakaji

konakaji commented Jul 8, 2025

Copy link
Copy Markdown

Hi @melody-ren

Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 834e286

Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test a431984

@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test ca0874d

Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test a8681eb

Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 0917714

Comment thread .github/workflows/all_libs.yaml Resolved
Comment thread .github/workflows/all_libs.yaml Resolved
Comment thread .github/workflows/build_wheels.yaml Resolved
Signed-off-by: Melody Ren <melodyr@nvidia.com>
Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 8211b74

Comment thread scripts/ci/test_wheels.sh Resolved
Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test 7e3ad67

@marwafar

Copy link
Copy Markdown
Collaborator

Could you also add notes may be as comment in the h2 example on what user should do if they just want to run it on single node multi-GPU. Currently, only for multi-node multi-GPU using mpi.

@bmhowe23 bmhowe23 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, Melody.

@melody-ren

Copy link
Copy Markdown
Collaborator Author

/ok to test a489590

@melody-ren

Copy link
Copy Markdown
Collaborator Author

Could you also add notes may be as comment in the h2 example on what user should do if they just want to run it on single node multi-GPU. Currently, only for multi-node multi-GPU using mpi.

Sure. Will address this in a follow-up PR. Tracked by issue #229

@melody-ren
melody-ren merged commit 6240a5e into NVIDIA:main Jul 22, 2025
melody-ren added a commit to melody-ren/cudaqx that referenced this pull request Jul 22, 2025
Signed-off-by: Melody Ren <melodyr@nvidia.com>
@melody-ren
melody-ren deleted the port_mgpu_gqe branch July 31, 2026 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants