Skip to content

Commit

Permalink
Fixing multi-GPU issue (#936)
Browse files Browse the repository at this point in the history
... and test tolerances
  • Loading branch information
bettinaheim committed Nov 16, 2023
1 parent a322940 commit 4d4cb8f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
36 changes: 21 additions & 15 deletions python/tests/unittests/test_kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,25 @@ def test_ctrl_h():
# controlled operation on the final qubit.
kernel.x(controls)
kernel.ch(controls, qubits[4])

counts = cudaq.sample(kernel)
print(counts)
counts1 = cudaq.sample(kernel)
print(counts1)

# Our first two qubits remain untouched, while `qubits[2]` is rotated
# to 1, and `qubits[3]` receives a Hadamard. This results in a nearly 50/50
# split of measurements on `qubits[3]` between 0 and 1.
# The controlled Hadamard on `qubits[4]` also results in a 50/50 split of its
# measurements between 0 and 1.
assert counts["0011011"] == 240
assert counts["0011111"] == 265
assert counts["0010011"] == 269
assert counts["0010111"] == 226
assert counts1["0011011"] >= 225 and counts1["0011011"] <= 275
assert counts1["0011111"] >= 225 and counts1["0011111"] <= 275
assert counts1["0010011"] >= 225 and counts1["0010011"] <= 275
assert counts1["0010111"] >= 225 and counts1["0010111"] <= 275
assert counts1["0011011"] + counts1["0011111"] + counts1["0010011"] + counts1["0010111"] == 1000

kernel.h(qubits[3])
kernel.h(qubits[4])
counts2 = cudaq.sample(kernel)
print(counts2)
assert counts2["0010011"] == 1000


def test_ctrl_s():
Expand Down Expand Up @@ -871,20 +877,20 @@ def test_fermionic_swap_op():
test_01.x(qubits_01[0])
test_01.fermionic_swap(angle, qubits_01[0], qubits_01[1])
ss_01 = cudaq.get_state(test_01)
assert np.isclose(np.abs(ss_01[1] - (-1j * np.exp(1j * angle / 2.0) * s)),
0.0, 1e-3)
assert np.isclose(np.abs(ss_01[2] - (np.exp(1j * angle / 2.0) * c)), 0.0,
1e-3)
val1 = np.abs(ss_01[1] - (-1j * np.exp(1j * angle / 2.0) * s))
val2 = np.abs(ss_01[2] - (np.exp(1j * angle / 2.0) * c))
assert np.isclose(val1, 0.0, atol=1e-6)
assert np.isclose(val2, 0.0, atol=1e-6)

test_10 = cudaq.make_kernel()
qubits_10 = test_10.qalloc(2)
test_10.x(qubits_10[1])
test_10.fermionic_swap(angle, qubits_10[0], qubits_10[1])
ss_10 = cudaq.get_state(test_10)
assert np.isclose(np.abs(ss_10[1] - (np.exp(1j * angle / 2.0) * c)), 0.0,
1e-3)
assert np.isclose(np.abs(ss_10[2] - (-1j * np.exp(1j * angle / 2.0) * s)),
0.0, 1e-3)
val3 = np.abs(ss_10[1] - (np.exp(1j * angle / 2.0) * c))
val4 = np.abs(ss_10[2] - (-1j * np.exp(1j * angle / 2.0) * s))
assert np.isclose(val3, 0.0, atol=1e-6)
assert np.isclose(val4, 0.0, atol=1e-6)


# leave for gdb debugging
Expand Down
14 changes: 7 additions & 7 deletions python/tests/unittests/test_observe.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_observe_no_params(want_state, want_expectation, shots_count):
spin_operator=hamiltonian,
shots_count=shots_count)
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# If shots mode was enabled, check those results.
if shots_count != -1:
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_observe_single_param(angle, want_state, want_expectation, shots_count):
angle,
shots_count=shots_count)
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# If shots mode was enabled, check those results.
if shots_count != -1:
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_observe_multi_param(angle_0, angle_1, angles, want_state,
angles,
shots_count=shots_count)
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# If shots mode was enabled, check those results.
if shots_count != -1:
Expand Down Expand Up @@ -321,7 +321,7 @@ def test_observe_async_no_params(want_state, want_expectation, shots_count):
shots_count=shots_count)
observe_result = future.get()
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# Test that this throws an exception, the problem here
# is we are on a quantum platform with 1 QPU, and we're asking
Expand Down Expand Up @@ -363,7 +363,7 @@ def test_observe_async_single_param(angle, want_state, want_expectation,
shots_count=shots_count)
observe_result = future.get()
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# If shots mode was enabled, check those results.
if shots_count != -1:
Expand Down Expand Up @@ -446,7 +446,7 @@ def test_observe_async_multi_param(angle_0, angle_1, angles, want_state,
shots_count=shots_count)
observe_result = future.get()
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# If shots mode was enabled, check those results.
if shots_count != -1:
Expand Down Expand Up @@ -534,7 +534,7 @@ def test_observe_numpy_array(angles, want_state, want_expectation):
numpy_angles,
shots_count=10)
got_expectation = observe_result.expectation()
assert want_expectation == got_expectation
assert np.isclose(want_expectation, got_expectation, atol=1e-12)

# Since shots mode was enabled, check the results.
sample_result = observe_result.counts()
Expand Down
2 changes: 1 addition & 1 deletion runtime/cudaq/algorithms/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ runSampling(KernelFunctor &&wrappedKernel, quantum_platform &platform,
// Trace the kernel function
ExecutionContext context("tracer");
auto &platform = get_platform();
platform.set_exec_ctx(&context);
platform.set_exec_ctx(&context, qpu_id);
wrappedKernel();
platform.reset_exec_ctx();
// In trace mode, if we have a measure result
Expand Down

0 comments on commit 4d4cb8f

Please sign in to comment.