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

Make sure uccsd compiles for quantum devices #2458

Merged
merged 76 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
00aa8d8
Temp
annagrin Nov 12, 2024
1a300ba
Merged with main
annagrin Dec 2, 2024
b1b220c
Add tests
annagrin Dec 9, 2024
0ca3674
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Dec 9, 2024
fac55bd
Revert temp changes
annagrin Dec 9, 2024
ae35d16
TEMP
annagrin Jan 7, 2025
ad486c1
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Jan 22, 2025
4178f0e
Fix bugs
annagrin Jan 29, 2025
cf283a3
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Jan 29, 2025
3c07cad
Add classic opts to LoopUnroll and add tests
annagrin Jan 30, 2025
ff887ea
Remove local mounts
annagrin Jan 30, 2025
718d9ed
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Jan 30, 2025
f33a3b9
Use .inc files instead of splitting patterns out
annagrin Jan 31, 2025
16d8d69
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Jan 31, 2025
0a36b6b
temp
annagrin Feb 4, 2025
ec5468e
temp
annagrin Feb 4, 2025
3dc023f
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin Feb 5, 2025
4648b9e
temp
annagrin Feb 5, 2025
150a84e
Add more patterns to loop-unroll
annagrin Feb 6, 2025
5a5f88a
Merge with main
annagrin Feb 6, 2025
591f09d
cleanup
annagrin Feb 7, 2025
377248b
cleanup
annagrin Feb 7, 2025
0ab3a56
Merge with main
annagrin Feb 7, 2025
f08502e
Fix some failing tests
annagrin Feb 7, 2025
2fb2cfe
Fix failing tests
annagrin Feb 7, 2025
6d4291f
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin Feb 7, 2025
e714387
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Feb 7, 2025
68b9ba2
Remove parts handled in other CRs
annagrin Feb 7, 2025
74178a8
Merged with main
annagrin Feb 7, 2025
ef0909c
Merged in loop-unroll-additional-opts
annagrin Feb 7, 2025
1e1a4ca
Cleanup
annagrin Feb 7, 2025
b3ec379
Fix test failures
annagrin Feb 8, 2025
9b6fc6a
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin Feb 8, 2025
3d3597d
Update write_after_write_elimination.qke
annagrin Feb 8, 2025
6a17b19
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Feb 8, 2025
b537ffe
Fix test failures
annagrin Feb 8, 2025
ee2884c
Update write_after_write_elimination.qke
annagrin Feb 8, 2025
7600781
Update test_kernel_features.py
annagrin Feb 8, 2025
e06980c
`DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Feb 8, 2025
032b465
Merge branch 'loop-unroll-additional-opts' of https://github.com/anna…
annagrin Feb 8, 2025
0ddeccf
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Feb 8, 2025
e74b60b
Remove unnecessary changes
annagrin Feb 8, 2025
aba4409
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Feb 8, 2025
1791226
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into lo…
annagrin Feb 8, 2025
a746d83
Fix failed tests
annagrin Feb 8, 2025
b944077
Fix failed tests
annagrin Feb 8, 2025
13cfcad
Fix failed tests
annagrin Feb 9, 2025
bfd4a4c
Run canonical patterns alongside lift-array-alloc
annagrin Feb 9, 2025
a9ec857
Fix failed tests
annagrin Feb 10, 2025
44c845b
Merge branch 'lift-array-alloc-array-copy' of https://github.com/anna…
annagrin Feb 10, 2025
3ce961f
Add classical-optimization pass and pipeline
annagrin Feb 11, 2025
0bdf458
Add more tests
annagrin Feb 11, 2025
0b58beb
Remove unneded comments
annagrin Feb 11, 2025
f9cca25
Remove unneded comments
annagrin Feb 11, 2025
0cff560
Merged with classical-optimization-pipeline2
annagrin Feb 11, 2025
ba7b46d
Use classical optimization pipeline in quantum config files
annagrin Feb 11, 2025
a62cbc7
Revert lift-array-alloc changes
annagrin Feb 11, 2025
96a62e6
Add classical-optimization-pipeline to more backends
annagrin Feb 11, 2025
3b93567
Merge branch 'main' into classical-optimization-pipeline2
annagrin Feb 12, 2025
5564a64
Fixed failing target config tests
annagrin Feb 12, 2025
b2cd5ce
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Feb 12, 2025
75f4cae
Merge branch 'classical-optimization-pipeline2' of https://github.com…
annagrin Feb 12, 2025
672d57f
Address CR comments
annagrin Feb 12, 2025
e178987
Cleanup
annagrin Feb 13, 2025
a93ba6e
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Feb 13, 2025
80e00c2
Merge with main
annagrin Feb 13, 2025
afb6238
Fix failing test
annagrin Feb 13, 2025
f64a45c
Fix failing tests
annagrin Feb 13, 2025
580e329
Fix failing tests
annagrin Feb 13, 2025
6c075be
fix failing tests
annagrin Feb 13, 2025
16e4e27
Fix failing tests
annagrin Feb 14, 2025
0f0fc21
Fix failing tests
annagrin Feb 14, 2025
246f8d9
DCO Remediation Commit for Anna Gringauze <[email protected]>
annagrin Feb 14, 2025
322269f
Add temp printing
annagrin Feb 14, 2025
f1e4d14
Fix failing tests
annagrin Feb 14, 2025
4408717
Merge branch 'main' of https://github.com/NVIDIA/cuda-quantum into fi…
annagrin Feb 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/cudaq/kernel/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def visit_Call(self, node):

if name not in globalAstRegistry:
raise RuntimeError(
f"{name} is not a valid kernel to call ({'.'.join(moduleNames)})."
f"{name} is not a valid kernel to call ({'.'.join(moduleNames)}). Registry: {globalAstRegistry}"
)

self.depKernels[name] = globalAstRegistry[name]
Expand Down
146 changes: 92 additions & 54 deletions python/cudaq/kernels/uccsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,37 @@ def uccsd_odd_electrons(qubits: cudaq.qview, thetas: list[float],
lenVirtA = len(virtual_alpha_indices)
lenVirtB = len(virtual_beta_indices)

singles_a = [[0, 0] for k in range(lenOccA * lenVirtA)]
singles_a0 = [0 for k in range(lenOccA * lenVirtA)]
singles_a1 = [0 for k in range(lenOccA * lenVirtA)]
counter = 0
for p in occupied_alpha_indices:
for q in virtual_alpha_indices:
singles_a[counter] = [p, q]
singles_a0[counter] = p
singles_a1[counter] = q
counter = counter + 1

counter = 0
singles_b = [[0, 0] for k in range(lenOccB * lenVirtB)]
singles_b0 = [0 for k in range(lenOccB * lenVirtB)]
singles_b1 = [0 for k in range(lenOccB * lenVirtB)]
for p in occupied_beta_indices:
for q in virtual_beta_indices:
singles_b[counter] = [p, q]
singles_b0[counter] = p
singles_b1[counter] = q
counter = counter + 1

counter = 0
doubles_m = [
[0, 0, 0, 0] for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)
]
doubles_m0 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m1 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m2 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m3 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
for p in occupied_alpha_indices:
for q in occupied_beta_indices:
for r in virtual_beta_indices:
for s in virtual_alpha_indices:
doubles_m[counter] = [p, q, r, s]
doubles_m0[counter] = p
doubles_m1[counter] = q
doubles_m2[counter] = r
doubles_m3[counter] = s
counter = counter + 1

counter = 0
Expand All @@ -384,13 +392,18 @@ def uccsd_odd_electrons(qubits: cudaq.qview, thetas: list[float],
nEle = nEle + 1

counter = 0
doubles_a = [[0, 0, 0, 0] for k in range(nEle)]
doubles_a0 = [0 for k in range(nEle)]
doubles_a1 = [0 for k in range(nEle)]
doubles_a2 = [0 for k in range(nEle)]
doubles_a3 = [0 for k in range(nEle)]
for p in range(lenOccA - 1):
for q in range(p + 1, lenOccA):
for r in range(lenVirtA - 1):
for s in range(r + 1, lenVirtA):
doubles_a[counter] = [occupied_alpha_indices[p],occupied_alpha_indices[q],\
virtual_alpha_indices[r],virtual_alpha_indices[s]]
doubles_a0[counter] = occupied_alpha_indices[p]
doubles_a1[counter] = occupied_alpha_indices[q]
doubles_a2[counter] = virtual_alpha_indices[r]
doubles_a3[counter] = virtual_alpha_indices[s]
counter = counter + 1

counter = 0
Expand All @@ -400,47 +413,53 @@ def uccsd_odd_electrons(qubits: cudaq.qview, thetas: list[float],
for r in range(lenVirtB - 1):
for s in range(r + 1, lenVirtB):
nEle = nEle + 1
doubles_b = [[0, 0, 0, 0] for k in range(nEle)]

doubles_b0 = [0 for k in range(nEle)]
doubles_b1 = [0 for k in range(nEle)]
doubles_b2 = [0 for k in range(nEle)]
doubles_b3 = [0 for k in range(nEle)]
for p in range(lenOccB - 1):
for q in range(p + 1, lenOccB):
for r in range(lenVirtB - 1):
for s in range(r + 1, lenVirtB):
doubles_b[counter] = [occupied_beta_indices[p],occupied_beta_indices[q],\
virtual_beta_indices[r],virtual_beta_indices[s]]
doubles_b0[counter] = occupied_beta_indices[p]
doubles_b1[counter] = occupied_beta_indices[q]
doubles_b2[counter] = virtual_beta_indices[r]
doubles_b3[counter] = virtual_beta_indices[s]
counter = counter + 1

n_alpha_singles = len(singles_a)
n_beta_singles = len(singles_b)
n_mixed_doubles = len(doubles_m)
n_alpha_doubles = len(doubles_a)
n_beta_doubles = len(doubles_b)
n_alpha_singles = len(singles_a0)
n_beta_singles = len(singles_b0)
n_mixed_doubles = len(doubles_m0)
n_alpha_doubles = len(doubles_a0)
n_beta_doubles = len(doubles_b0)

thetaCounter = 0
for i in range(n_alpha_singles):
single_excitation(qubits, singles_a[i][0], singles_a[i][1],
single_excitation(qubits, singles_a0[i], singles_a1[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_beta_singles):
single_excitation(qubits, singles_b[i][0], singles_b[i][1],
single_excitation(qubits, singles_b0[i], singles_b1[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_mixed_doubles):
double_excitation_opt(qubits, doubles_m[i][0], doubles_m[i][1],
doubles_m[i][2], doubles_m[i][3],
double_excitation_opt(qubits, doubles_m0[i], doubles_m1[i],
doubles_m2[i], doubles_m3[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_alpha_doubles):
double_excitation_opt(qubits, doubles_a[i][0], doubles_a[i][1],
doubles_a[i][2], doubles_a[i][3],
double_excitation_opt(qubits, doubles_a0[i], doubles_a1[i],
doubles_a2[i], doubles_a3[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_beta_doubles):
double_excitation_opt(qubits, doubles_b[i][0], doubles_b[i][1],
doubles_b[i][2], doubles_b[i][3],
double_excitation_opt(qubits, doubles_b0[i], doubles_b1[i],
doubles_b2[i], doubles_b3[i],
thetas[thetaCounter])
thetaCounter += 1

Expand All @@ -463,29 +482,37 @@ def uccsd_even_electrons(qubits: cudaq.qview, thetas: list[float],
lenVirtA = len(virtual_alpha_indices)
lenVirtB = len(virtual_beta_indices)

singles_a = [[0, 0] for k in range(lenOccA * lenVirtA)]
singles_a0 = [0 for k in range(lenOccA * lenVirtA)]
singles_a1 = [0 for k in range(lenOccA * lenVirtA)]
counter = 0
for p in occupied_alpha_indices:
for q in virtual_alpha_indices:
singles_a[counter] = [p, q]
singles_a0[counter] = p
singles_a1[counter] = q
counter = counter + 1

counter = 0
singles_b = [[0, 0] for k in range(lenOccB * lenVirtB)]
singles_b0 = [0 for k in range(lenOccB * lenVirtB)]
singles_b1 = [0 for k in range(lenOccB * lenVirtB)]
for p in occupied_beta_indices:
for q in virtual_beta_indices:
singles_b[counter] = [p, q]
singles_b0[counter] = p
singles_b1[counter] = q
counter = counter + 1

counter = 0
doubles_m = [
[0, 0, 0, 0] for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)
]
doubles_m0 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m1 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m2 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
doubles_m3 = [0 for k in range(lenOccB * lenVirtB * lenOccA * lenVirtA)]
for p in occupied_alpha_indices:
for q in occupied_beta_indices:
for r in virtual_beta_indices:
for s in virtual_alpha_indices:
doubles_m[counter] = [p, q, r, s]
doubles_m0[counter] = p
doubles_m1[counter] = q
doubles_m2[counter] = r
doubles_m3[counter] = s
counter = counter + 1

counter = 0
Expand All @@ -497,13 +524,18 @@ def uccsd_even_electrons(qubits: cudaq.qview, thetas: list[float],
nEle = nEle + 1

counter = 0
doubles_a = [[0, 0, 0, 0] for k in range(nEle)]
doubles_a0 = [0 for k in range(nEle)]
doubles_a1 = [0 for k in range(nEle)]
doubles_a2 = [0 for k in range(nEle)]
doubles_a3 = [0 for k in range(nEle)]
for p in range(lenOccA - 1):
for q in range(p + 1, lenOccA):
for r in range(lenVirtA - 1):
for s in range(r + 1, lenVirtA):
doubles_a[counter] = [occupied_alpha_indices[p],occupied_alpha_indices[q],\
virtual_alpha_indices[r],virtual_alpha_indices[s]]
doubles_a0[counter] = occupied_alpha_indices[p]
doubles_a1[counter] = occupied_alpha_indices[q]
doubles_a2[counter] = virtual_alpha_indices[r]
doubles_a3[counter] = virtual_alpha_indices[s]
counter = counter + 1

counter = 0
Expand All @@ -513,47 +545,53 @@ def uccsd_even_electrons(qubits: cudaq.qview, thetas: list[float],
for r in range(lenVirtB - 1):
for s in range(r + 1, lenVirtB):
nEle = nEle + 1
doubles_b = [[0, 0, 0, 0] for k in range(nEle)]

doubles_b0 = [0 for k in range(nEle)]
doubles_b1 = [0 for k in range(nEle)]
doubles_b2 = [0 for k in range(nEle)]
doubles_b3 = [0 for k in range(nEle)]
for p in range(lenOccB - 1):
for q in range(p + 1, lenOccB):
for r in range(lenVirtB - 1):
for s in range(r + 1, lenVirtB):
doubles_b[counter] = [occupied_beta_indices[p],occupied_beta_indices[q],\
virtual_beta_indices[r],virtual_beta_indices[s]]
doubles_b0[counter] = occupied_beta_indices[p]
doubles_b1[counter] = occupied_beta_indices[q]
doubles_b2[counter] = virtual_beta_indices[r]
doubles_b3[counter] = virtual_beta_indices[s]
counter = counter + 1

n_alpha_singles = len(singles_a)
n_beta_singles = len(singles_b)
n_mixed_doubles = len(doubles_m)
n_alpha_doubles = len(doubles_a)
n_beta_doubles = len(doubles_b)
n_alpha_singles = len(singles_a0)
n_beta_singles = len(singles_b0)
n_mixed_doubles = len(doubles_m0)
n_alpha_doubles = len(doubles_a0)
n_beta_doubles = len(doubles_b0)

thetaCounter = 0
for i in range(n_alpha_singles):
single_excitation(qubits, singles_a[i][0], singles_a[i][1],
single_excitation(qubits, singles_a0[i], singles_a1[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_beta_singles):
single_excitation(qubits, singles_b[i][0], singles_b[i][1],
single_excitation(qubits, singles_b0[i], singles_b1[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_mixed_doubles):
double_excitation_opt(qubits, doubles_m[i][0], doubles_m[i][1],
doubles_m[i][2], doubles_m[i][3],
double_excitation_opt(qubits, doubles_m0[i], doubles_m1[i],
doubles_m2[i], doubles_m3[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_alpha_doubles):
double_excitation_opt(qubits, doubles_a[i][0], doubles_a[i][1],
doubles_a[i][2], doubles_a[i][3],
double_excitation_opt(qubits, doubles_a0[i], doubles_a1[i],
doubles_a2[i], doubles_a3[i],
thetas[thetaCounter])
thetaCounter += 1

for i in range(n_beta_doubles):
double_excitation_opt(qubits, doubles_b[i][0], doubles_b[i][1],
doubles_b[i][2], doubles_b[i][3],
double_excitation_opt(qubits, doubles_b0[i], doubles_b1[i],
doubles_b2[i], doubles_b3[i],
thetas[thetaCounter])
thetaCounter += 1

Expand Down
8 changes: 8 additions & 0 deletions python/runtime/cudaq/platform/py_alt_launch_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ jitAndCreateArgs(const std::string &name, MlirModule module,
pm.addPass(createSymbolDCEPass());
cudaq::opt::addPipelineConvertToQIR(pm);

auto enablePrintMLIREachPass =
getEnvBool("CUDAQ_MLIR_PRINT_EACH_PASS", false);

if (enablePrintMLIREachPass) {
cloned.getContext()->disableMultithreading();
pm.enableIRPrinting();
}

DefaultTimingManager tm;
tm.setEnabled(cudaq::isTimingTagEnabled(cudaq::TIMING_JIT_PASSES));
auto timingScope = tm.getRootScope(); // starts the timer
Expand Down
62 changes: 62 additions & 0 deletions python/tests/backends/test_Ionq_LocalEmulation_kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# ============================================================================ #
# Copyright (c) 2022 - 2025 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. #
# ============================================================================ #

import cudaq
import cudaq.kernels
import pytest
import os
from typing import List


@pytest.fixture(scope="function", autouse=True)
def configureTarget():
# Set the targeted QPU
cudaq.set_target('ionq', emulate='true')

yield "Running the tests."

cudaq.reset_target()


def test_Ionq_cudaq_uccsd():

num_electrons = 2
num_qubits = 8

thetas = [
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558,
-0.00037043841404585794, 0.0003811110195084151, 0.2286823796532558
]

@cudaq.kernel
def kernel():
qubits = cudaq.qvector(num_qubits)
for i in range(num_electrons):
x(qubits[i])
cudaq.kernels.uccsd(qubits, thetas, num_electrons, num_qubits)

counts = cudaq.sample(kernel, shots_count=1000)
assert len(counts) == 6
assert '00000011' in counts
assert '00000110' in counts
assert '00010010' in counts
assert '01000010' in counts
assert '10000001' in counts
assert '11000000' in counts


# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
pytest.main([loc, "-s"])
Loading
Loading