Skip to content

Commit

Permalink
Merge branch 'main' into tnguyen/issue-2537
Browse files Browse the repository at this point in the history
  • Loading branch information
1tnguyen authored Feb 3, 2025
2 parents 96f02f2 + 3b0f04c commit 3052c5a
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 113 deletions.
12 changes: 7 additions & 5 deletions python/cudaq/kernel/ast_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,8 @@ def visit_Assign(self, node):
self.subscriptPushPointerValue = True
# Visit the subscript node, get the pointer value
self.visit(node.targets[0])
# Reset the push pointer value flag
self.subscriptPushPointerValue = False
ptrVal = self.popValue()
if not cc.PointerType.isinstance(ptrVal.type):
self.emitFatalError(
Expand All @@ -1108,8 +1110,6 @@ def visit_Assign(self, node):
valueToStore = self.popValue()
# Store the value
cc.StoreOp(valueToStore, ptrVal)
# Reset the push pointer value flag
self.subscriptPushPointerValue = False
return

else:
Expand Down Expand Up @@ -1456,11 +1456,13 @@ def get_full_module_path(partial_path):

# The total number of elements in the iterable
# we are generating should be `N == endVal - startVal`
totalSize = math.AbsIOp(arith.SubIOp(endVal,
startVal).result).result
actualSize = arith.SubIOp(endVal, startVal).result
totalSize = math.AbsIOp(actualSize).result

# If the step is not == 1, then we also have
# to update the total size for the range iterable
actualSize = arith.DivSIOp(actualSize,
math.AbsIOp(stepVal).result).result
totalSize = arith.DivSIOp(totalSize,
math.AbsIOp(stepVal).result).result

Expand Down Expand Up @@ -1499,7 +1501,7 @@ def bodyBuilder(iterVar):
isDecrementing=isDecrementing)

self.pushValue(iterable)
self.pushValue(totalSize)
self.pushValue(actualSize)
return

if node.func.id == 'enumerate':
Expand Down
3 changes: 1 addition & 2 deletions python/tests/backends/test_braket.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

@pytest.fixture(scope="session", autouse=True)
def do_something():
device_arn = "arn:aws:braket:::device/quantum-simulator/amazon/sv1"
cudaq.set_target("braket", machine=device_arn)
cudaq.set_target("braket")
yield "Running the tests."
cudaq.__clearKernelRegistries()
cudaq.reset_target()
Expand Down
68 changes: 68 additions & 0 deletions python/tests/kernel/test_kernel_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,74 @@ def kernel(myList: list[int]):
assert '1' * 5 in counts


def test_list_boundaries():

@cudaq.kernel
def kernel1():
qubits = cudaq.qvector(2)
r = range(0, 0)
for i in r:
x(qubits[i])

counts = cudaq.sample(kernel1)
assert len(counts) == 1
assert '00' in counts

@cudaq.kernel
def kernel2():
qubits = cudaq.qvector(2)
r = range(1, 0)
for i in r:
x(qubits[i])

counts = cudaq.sample(kernel2)
assert len(counts) == 1
assert '00' in counts

@cudaq.kernel
def kernel3():
qubits = cudaq.qvector(2)
for i in range(-1):
x(qubits[i])

counts = cudaq.sample(kernel3)
assert len(counts) == 1
assert '00' in counts

@cudaq.kernel
def kernel4():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(-1)]
for i in r:
x(qubits[i])

counts = cudaq.sample(kernel4)
assert len(counts) == 1
assert '0000' in counts

@cudaq.kernel
def kernel5():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(0)]
for i in r:
x(qubits[i])

counts = cudaq.sample(kernel5)
assert len(counts) == 1
assert '0000' in counts

@cudaq.kernel
def kernel6():
qubits = cudaq.qvector(4)
r = [i * 2 + 1 for i in range(2)]
for i in r:
x(qubits[i])

counts = cudaq.sample(kernel6)
assert len(counts) == 1
assert '0101' in counts


def test_control_operations():

@cudaq.kernel
Expand Down
27 changes: 12 additions & 15 deletions python/tests/mlir/test_output_qir.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def ghz(numQubits: int):
print(cudaq.translate(ghz_synth, format='qir-base'))


# CHECK: %[[VAL_0:.*]] = tail call
# CHECK: %[[VAL_1:.*]]* @__quantum__rt__qubit_allocate_array(i64
# CHECK: %[[VAL_2:.*]])
# CHECK: %[[VAL_0:.*]] = tail call %[[VAL_1:.*]]* @__quantum__rt__qubit_allocate_array(i64 %[[VAL_2:.*]])
# CHECK: %[[VAL_3:.*]] = tail call i8* @__quantum__rt__array_get_element_ptr_1d(%[[VAL_1]]* %[[VAL_0]], i64 0)
# CHECK: %[[VAL_4:.*]] = bitcast i8* %[[VAL_3]] to %[[VAL_5:.*]]**
# CHECK: %[[VAL_6:.*]] = load %[[VAL_5]]*, %[[VAL_5]]** %[[VAL_4]], align 8
Expand All @@ -43,25 +41,24 @@ def ghz(numQubits: int):
# CHECK: store i64 %[[VAL_14]], i64* %[[VAL_16]], align 8
# CHECK: %[[VAL_15]] = add nuw nsw i64 %[[VAL_14]], 1
# CHECK: %[[VAL_17:.*]] = icmp slt i64 %[[VAL_15]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_17]], label %[[VAL_11]], label %[[VAL_12]]
# CHECK: ._crit_edge: ; preds = %[[VAL_11]], %[[VAL_13]]
# CHECK: %[[VAL_18:.*]] = alloca { i64, i64 }, i64 %[[VAL_8]], align 8
# CHECK: %[[VAL_19:.*]] = icmp sgt i64 %[[VAL_8]], 0
# CHECK: br i1 %[[VAL_19]], label %[[VAL_20:.*]], label %[[VAL_21:.*]]
# CHECK: br i1 %[[VAL_17]], label %[[VAL_11]], label %[[VAL_21:.*]]
# CHECK: ._crit_edge: ; preds = %[[VAL_11]]
# CHECK: %[[VAL_18:.*]] = alloca { i64, i64 }, i64 %[[VAL_7]], align 8
# CHECK: br i1 %[[VAL_10]], label %[[VAL_20:.*]], label %[[VAL_21]]
# CHECK: .preheader: ; preds = %[[VAL_20]]
# CHECK: br i1 %[[VAL_19]], label %[[VAL_22:.*]], label %[[VAL_21]]
# CHECK: .lr.ph9: ; preds = %[[VAL_12]], %[[VAL_20]]
# CHECK: %[[VAL_23:.*]] = phi i64 [ %[[VAL_24:.*]], %[[VAL_20]] ], [ 0, %[[VAL_12]] ]
# CHECK: br i1 %[[VAL_10]], label %[[VAL_22:.*]], label %[[VAL_21]]
# CHECK: .lr.ph10: ; preds = %[[VAL_21]], %[[VAL_20]]
# CHECK: %[[VAL_23:.*]] = phi i64 [ %[[VAL_24:.*]], %[[VAL_20]] ], [ 0, %[[VAL_21]] ]
# CHECK: %[[VAL_25:.*]] = getelementptr i64, i64* %[[VAL_9]], i64 %[[VAL_23]]
# CHECK: %[[VAL_26:.*]] = load i64, i64* %[[VAL_25]], align 8
# CHECK: %[[VAL_27:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_23]], i32 0
# CHECK: store i64 %[[VAL_23]], i64* %[[VAL_27]], align 8
# CHECK: %[[VAL_28:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_23]], i32 1
# CHECK: store i64 %[[VAL_26]], i64* %[[VAL_28]], align 8
# CHECK: %[[VAL_24]] = add nuw nsw i64 %[[VAL_23]], 1
# CHECK: %[[VAL_29:.*]] = icmp slt i64 %[[VAL_24]], %[[VAL_8]]
# CHECK: %[[VAL_29:.*]] = icmp slt i64 %[[VAL_24]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_29]], label %[[VAL_20]], label %[[VAL_30:.*]]
# CHECK: .lr.ph10: ; preds = %[[VAL_30]], %[[VAL_22]]
# CHECK: .lr.ph11: ; preds = %[[VAL_30]], %[[VAL_22]]
# CHECK: %[[VAL_31:.*]] = phi i64 [ %[[VAL_32:.*]], %[[VAL_22]] ], [ 0, %[[VAL_30]] ]
# CHECK: %[[VAL_33:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_31]], i32 0
# CHECK: %[[VAL_34:.*]] = load i64, i64* %[[VAL_33]], align 8
Expand All @@ -76,9 +73,9 @@ def ghz(numQubits: int):
# CHECK: %[[VAL_43:.*]] = load %[[VAL_5]]*, %[[VAL_5]]** %[[VAL_42]], align 8
# CHECK: tail call void (i64, void (%[[VAL_1]]*, %[[VAL_5]]*)*, ...) @invokeWithControlQubits(i64 1, void (%[[VAL_1]]*, %[[VAL_5]]*)* nonnull @__quantum__qis__x__ctl, %[[VAL_5]]* %[[VAL_39]], %[[VAL_5]]* %[[VAL_43]])
# CHECK: %[[VAL_32]] = add nuw nsw i64 %[[VAL_31]], 1
# CHECK: %[[VAL_44:.*]] = icmp slt i64 %[[VAL_32]], %[[VAL_8]]
# CHECK: %[[VAL_44:.*]] = icmp slt i64 %[[VAL_32]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_44]], label %[[VAL_22]], label %[[VAL_21]]
# CHECK: ._crit_edge11: ; preds = %[[VAL_22]], %[[VAL_12]], %[[VAL_30]]
# CHECK: ._crit_edge12: ; preds = %[[VAL_22]], %[[VAL_13]], %[[VAL_21]], %[[VAL_30]]
# CHECK: tail call void @__quantum__rt__qubit_release_array(%[[VAL_1]]* %[[VAL_0]])
# CHECK: ret void

Expand Down
27 changes: 12 additions & 15 deletions python/tests/mlir/test_output_translate_qir.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def ghz(numQubits: int):
print(cudaq.translate(ghz_synth, format='qir-base'))


# CHECK: %[[VAL_0:.*]] = tail call
# CHECK: %[[VAL_1:.*]]* @__quantum__rt__qubit_allocate_array(i64
# CHECK: %[[VAL_2:.*]])
# CHECK: %[[VAL_0:.*]] = tail call %[[VAL_1:.*]]* @__quantum__rt__qubit_allocate_array(i64 %[[VAL_2:.*]])
# CHECK: %[[VAL_3:.*]] = tail call i8* @__quantum__rt__array_get_element_ptr_1d(%[[VAL_1]]* %[[VAL_0]], i64 0)
# CHECK: %[[VAL_4:.*]] = bitcast i8* %[[VAL_3]] to %[[VAL_5:.*]]**
# CHECK: %[[VAL_6:.*]] = load %[[VAL_5]]*, %[[VAL_5]]** %[[VAL_4]], align 8
Expand All @@ -43,25 +41,24 @@ def ghz(numQubits: int):
# CHECK: store i64 %[[VAL_14]], i64* %[[VAL_16]], align 8
# CHECK: %[[VAL_15]] = add nuw nsw i64 %[[VAL_14]], 1
# CHECK: %[[VAL_17:.*]] = icmp slt i64 %[[VAL_15]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_17]], label %[[VAL_11]], label %[[VAL_12]]
# CHECK: ._crit_edge: ; preds = %[[VAL_11]], %[[VAL_13]]
# CHECK: %[[VAL_18:.*]] = alloca { i64, i64 }, i64 %[[VAL_8]], align 8
# CHECK: %[[VAL_19:.*]] = icmp sgt i64 %[[VAL_8]], 0
# CHECK: br i1 %[[VAL_19]], label %[[VAL_20:.*]], label %[[VAL_21:.*]]
# CHECK: br i1 %[[VAL_17]], label %[[VAL_11]], label %[[VAL_21:.*]]
# CHECK: ._crit_edge: ; preds = %[[VAL_11]]
# CHECK: %[[VAL_18:.*]] = alloca { i64, i64 }, i64 %[[VAL_7]], align 8
# CHECK: br i1 %[[VAL_10]], label %[[VAL_20:.*]], label %[[VAL_21]]
# CHECK: .preheader: ; preds = %[[VAL_20]]
# CHECK: br i1 %[[VAL_19]], label %[[VAL_22:.*]], label %[[VAL_21]]
# CHECK: .lr.ph9: ; preds = %[[VAL_12]], %[[VAL_20]]
# CHECK: %[[VAL_23:.*]] = phi i64 [ %[[VAL_24:.*]], %[[VAL_20]] ], [ 0, %[[VAL_12]] ]
# CHECK: br i1 %[[VAL_10]], label %[[VAL_22:.*]], label %[[VAL_21]]
# CHECK: .lr.ph10: ; preds = %[[VAL_21]], %[[VAL_20]]
# CHECK: %[[VAL_23:.*]] = phi i64 [ %[[VAL_24:.*]], %[[VAL_20]] ], [ 0, %[[VAL_21]] ]
# CHECK: %[[VAL_25:.*]] = getelementptr i64, i64* %[[VAL_9]], i64 %[[VAL_23]]
# CHECK: %[[VAL_26:.*]] = load i64, i64* %[[VAL_25]], align 8
# CHECK: %[[VAL_27:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_23]], i32 0
# CHECK: store i64 %[[VAL_23]], i64* %[[VAL_27]], align 8
# CHECK: %[[VAL_28:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_23]], i32 1
# CHECK: store i64 %[[VAL_26]], i64* %[[VAL_28]], align 8
# CHECK: %[[VAL_24]] = add nuw nsw i64 %[[VAL_23]], 1
# CHECK: %[[VAL_29:.*]] = icmp slt i64 %[[VAL_24]], %[[VAL_8]]
# CHECK: %[[VAL_29:.*]] = icmp slt i64 %[[VAL_24]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_29]], label %[[VAL_20]], label %[[VAL_30:.*]]
# CHECK: .lr.ph10: ; preds = %[[VAL_30]], %[[VAL_22]]
# CHECK: .lr.ph11: ; preds = %[[VAL_30]], %[[VAL_22]]
# CHECK: %[[VAL_31:.*]] = phi i64 [ %[[VAL_32:.*]], %[[VAL_22]] ], [ 0, %[[VAL_30]] ]
# CHECK: %[[VAL_33:.*]] = getelementptr { i64, i64 }, { i64, i64 }* %[[VAL_18]], i64 %[[VAL_31]], i32 0
# CHECK: %[[VAL_34:.*]] = load i64, i64* %[[VAL_33]], align 8
Expand All @@ -76,9 +73,9 @@ def ghz(numQubits: int):
# CHECK: %[[VAL_43:.*]] = load %[[VAL_5]]*, %[[VAL_5]]** %[[VAL_42]], align 8
# CHECK: tail call void (i64, void (%[[VAL_1]]*, %[[VAL_5]]*)*, ...) @invokeWithControlQubits(i64 1, void (%[[VAL_1]]*, %[[VAL_5]]*)* nonnull @__quantum__qis__x__ctl, %[[VAL_5]]* %[[VAL_39]], %[[VAL_5]]* %[[VAL_43]])
# CHECK: %[[VAL_32]] = add nuw nsw i64 %[[VAL_31]], 1
# CHECK: %[[VAL_44:.*]] = icmp slt i64 %[[VAL_32]], %[[VAL_8]]
# CHECK: %[[VAL_44:.*]] = icmp slt i64 %[[VAL_32]], %[[VAL_7]]
# CHECK: br i1 %[[VAL_44]], label %[[VAL_22]], label %[[VAL_21]]
# CHECK: ._crit_edge11: ; preds = %[[VAL_22]], %[[VAL_12]], %[[VAL_30]]
# CHECK: ._crit_edge12: ; preds = %[[VAL_22]], %[[VAL_13]], %[[VAL_21]], %[[VAL_30]]
# CHECK: tail call void @__quantum__rt__qubit_release_array(%[[VAL_1]]* %[[VAL_0]])
# CHECK: ret void

Expand Down
34 changes: 0 additions & 34 deletions runtime/common/BraketServerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,6 @@

namespace cudaq {

const std::string SV1 = "sv1";
const std::string DM1 = "dm1";
const std::string TN1 = "tn1";
const std::string ARIA1 = "aria1";
const std::string ARIA2 = "aria2";
const std::string GARNET = "garnet";
const std::string AQUILA = "aquila";

const std::string SV1_ARN =
"arn:aws:braket:::device/quantum-simulator/amazon/sv1";
const std::string DM1_ARN =
"arn:aws:braket:::device/quantum-simulator/amazon/dm1";
const std::string TN1_ARN =
"arn:aws:braket:::device/quantum-simulator/amazon/tn1";
const std::string ARIA1_ARN =
"arn:aws:braket:us-east-1::device/qpu/ionq/Aria-1";
const std::string ARIA2_ARN =
"arn:aws:braket:us-east-1::device/qpu/ionq/Aria-2";
const std::string GARNET_ARN =
"arn:aws:braket:eu-north-1::device/qpu/iqm/Garnet";
const std::string AQUILA_ARN =
"arn:aws:braket:us-east-1::device/qpu/quera/Aquila";

const std::map<std::string, std::string> deviceArns = {
{SV1, SV1_ARN}, {DM1, DM1_ARN}, {TN1, TN1_ARN},
{ARIA1, ARIA1_ARN}, {ARIA2, ARIA2_ARN}, {GARNET, GARNET_ARN},
{AQUILA, AQUILA_ARN}};

const std::map<std::string, uint> deviceQubitCounts = {
{SV1_ARN, 34}, {DM1_ARN, 17}, {TN1_ARN, 50}, {ARIA1_ARN, 25},
{ARIA2_ARN, 25}, {GARNET_ARN, 20}, {AQUILA_ARN, 256}};

const uint DEFAULT_QUBIT_COUNT = 50;

/// @brief The BraketServerHelper class extends the ServerHelper class to handle
/// interactions with the Amazon Braket server for submitting and retrieving
/// quantum computation jobs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,12 @@ std::string prepareOpenQasm(std::string source) {

namespace cudaq {

std::string getDeviceArn(const std::string &machine) {
if (machine.starts_with("arn:aws:braket")) {
std::string checkDeviceArn(const std::string &machine) {
if (machine.starts_with("arn:aws:braket"))
return machine;
}

if (deviceArns.contains(machine)) {
return deviceArns.at(machine);
}

std::string knownMachines;
for (const auto &machine : deviceArns)
knownMachines += machine.first + " ";
const auto errorMessage =
fmt::format("Machine \"{}\" is invalid. Machine must be either an Amazon "
"Braket device ARN or one of the known devices: {}",
machine, knownMachines);
const auto errorMessage = fmt::format("Machine \"{}\" is invalid. Machine "
"must be an Amazon Braket device ARN.",
machine);
throw std::runtime_error(errorMessage);
}

Expand All @@ -51,31 +41,25 @@ BraketServerHelper::getValueOrDefault(const BackendConfig &config,
// Initialize the Braket server helper with a given backend configuration
void BraketServerHelper::initialize(BackendConfig config) {
cudaq::info("Initializing Amazon Braket backend.");

// Fetch machine info before checking emulate because we want to be able to
// emulate specific machines.
auto machine = getValueOrDefault(config, "machine", SV1);
auto deviceArn = getDeviceArn(machine);
// emulate specific machines, defaults to state vector simulator
auto machine =
getValueOrDefault(config, "machine",
"arn:aws:braket:::device/quantum-simulator/amazon/sv1");
auto deviceArn = checkDeviceArn(machine);
cudaq::info("Running on device {}", deviceArn);

config["defaultBucket"] = getValueOrDefault(config, "default_bucket", "");
config["deviceArn"] = deviceArn;
config["qubits"] = deviceQubitCounts.contains(deviceArn)
? deviceQubitCounts.at(deviceArn)
: DEFAULT_QUBIT_COUNT;
if (!config["shots"].empty())
this->setShots(std::stoul(config["shots"]));

const auto emulate_it = config.find("emulate");
if (emulate_it != config.end() && emulate_it->second == "true") {
cudaq::info("Emulation is enabled, ignore all Amazon Braket connection "
"specific information.");
backendConfig = std::move(config);
return;
}

parseConfigForCommonParams(config);

// Move the passed config into the member variable backendConfig
backendConfig = std::move(config);
};
Expand All @@ -90,25 +74,19 @@ BraketServerHelper::createJob(std::vector<KernelExecution> &circuitCodes) {
ServerMessage taskRequest;
taskRequest["name"] = circuitCode.name;
taskRequest["deviceArn"] = backendConfig.at("deviceArn");

taskRequest["qubits"] = backendConfig.at("qubits");
taskRequest["input"]["format"] = "qasm2";
taskRequest["input"]["data"] = circuitCode.code;

auto action = nlohmann::json::parse(
"{\"braketSchemaHeader\": {\"name\": \"braket.ir.openqasm.program\", "
"\"version\": \"1\"}, \"source\": \"\", \"inputs\": {}}");
action["source"] = prepareOpenQasm(circuitCode.code);
taskRequest["action"] = action.dump();
taskRequest["shots"] = shots;

tasks.push_back(taskRequest);
}

cudaq::info("Created job payload for braket, language is OpenQASM 2.0, "
"targeting device {}",
backendConfig.at("deviceArn"));

return ret;
};

Expand Down
Loading

0 comments on commit 3052c5a

Please sign in to comment.