Skip to content

Commit

Permalink
Merge branch 'main' into ch-remove.peephole
Browse files Browse the repository at this point in the history
  • Loading branch information
schweitzpgi authored Dec 13, 2024
2 parents 31bedfa + ec5f15e commit 1987897
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[style]
based_on_style = google
7 changes: 5 additions & 2 deletions include/cudaq/Optimizer/Dialect/Quake/QuakeTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,12 @@ def VeqType : QuakeType<"Veq", "veq"> {
let hasCustomAssemblyFormat = 1;

let extraClassDeclaration = [{
bool hasSpecifiedSize() const { return getSize(); }
static constexpr std::size_t kDynamicSize =
std::numeric_limits<std::size_t>::max();

bool hasSpecifiedSize() const { return getSize() != kDynamicSize; }
static VeqType getUnsized(mlir::MLIRContext *ctx) {
return VeqType::get(ctx, 0);
return VeqType::get(ctx, kDynamicSize);
}
}];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Optimizer/Dialect/Quake/QuakeTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void quake::VeqType::print(AsmPrinter &os) const {
Type quake::VeqType::parse(AsmParser &parser) {
if (parser.parseLess())
return {};
std::size_t size = 0;
std::size_t size = kDynamicSize;
if (succeeded(parser.parseOptionalQuestion()))
size = 0;
size = kDynamicSize;
else if (parser.parseInteger(size))
return {};
if (parser.parseGreater())
Expand Down
2 changes: 1 addition & 1 deletion python/cudaq/kernel/ast_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3168,7 +3168,7 @@ def bodyBuilder(iterVar):
# we currently handle `veq` and `stdvec` types
if quake.VeqType.isinstance(iterable.type):
size = quake.VeqType.getSize(iterable.type)
if size:
if quake.VeqType.hasSpecifiedSize(iterable.type):
totalSize = self.getConstantInt(size)
else:
totalSize = quake.VeqSizeOp(self.getIntegerType(64),
Expand Down
8 changes: 4 additions & 4 deletions python/cudaq/kernel/kernel_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@ def __applyControlOrAdjoint(self, target, isAdjoint, controls, *args):

if (quake.VeqType.isinstance(inTy) and
quake.VeqType.isinstance(argTy)):
if quake.VeqType.getSize(
inTy) and not quake.VeqType.getSize(argTy):
if quake.VeqType.hasSpecifiedSize(
inTy) and not quake.VeqType.hasSpecifiedSize(argTy):
value = quake.RelaxSizeOp(argTy, value).result

mlirValues.append(value)
Expand Down Expand Up @@ -1029,8 +1029,8 @@ def reset(self, target):
return

# target is a VeqType
size = quake.VeqType.getSize(target.mlirValue.type)
if size:
if quake.VeqType.hasSpecifiedSize(target.mlirValue.type):
size = quake.VeqType.getSize(target.mlirValue.type)
for i in range(size):
extracted = quake.ExtractRefOp(quake.RefType.get(self.ctx),
target.mlirValue, i).result
Expand Down
2 changes: 1 addition & 1 deletion python/cudaq/kernel/quake_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def size(self):

if quake.VeqType.isinstance(type):
size = quake.VeqType.getSize(type)
if size:
if quake.VeqType.hasSpecifiedSize(type):
return size
return QuakeValue(
quake.VeqSizeOp(self.intType, self.mlirValue).result,
Expand Down
3 changes: 2 additions & 1 deletion python/runtime/mlir/py_register_dialects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void registerQuakeDialectAndTypes(py::module &m) {
[](py::object cls, MlirContext ctx, std::size_t size) {
return wrap(quake::VeqType::get(unwrap(ctx), size));
},
py::arg("cls"), py::arg("context"), py::arg("size") = 0)
py::arg("cls"), py::arg("context"),
py::arg("size") = std::numeric_limits<std::size_t>::max())
.def_staticmethod(
"hasSpecifiedSize",
[](MlirType type) {
Expand Down
3 changes: 2 additions & 1 deletion runtime/cudaq/builder/QuakeValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ QuakeValue QuakeValue::size() {

std::optional<std::size_t> QuakeValue::constantSize() {
if (auto qvecTy = dyn_cast<quake::VeqType>(getValue().getType()))
return qvecTy.getSize();
if (qvecTy.hasSpecifiedSize())
return qvecTy.getSize();

return std::nullopt;
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/cudaq/platform/default/DefaultQuantumPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class DefaultQuantumPlatform : public cudaq::quantum_platform {
/// specified by that variable.
void setTargetBackend(const std::string &backend) override {
platformQPUs.clear();
threadToQpuId.clear();
platformQPUs.emplace_back(std::make_unique<DefaultQPU>());

cudaq::info("Backend string is {}", backend);
Expand Down Expand Up @@ -121,6 +122,7 @@ class DefaultQuantumPlatform : public cudaq::quantum_platform {
auto qpuName = config.BackendConfig->PlatformQpu;
cudaq::info("Default platform QPU subtype name: {}", qpuName);
platformQPUs.clear();
threadToQpuId.clear();
platformQPUs.emplace_back(cudaq::registry::get<cudaq::QPU>(qpuName));
if (platformQPUs.front() == nullptr)
throw std::runtime_error(
Expand Down
4 changes: 4 additions & 0 deletions runtime/cudaq/platform/mqpu/MultiQPUPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MultiQPUQuantumPlatform : public cudaq::quantum_platform {
// Make sure that we clean up the client QPUs first before cleaning up the
// remote servers.
platformQPUs.clear();
threadToQpuId.clear();
platformNumQPUs = 0;
m_remoteServers.clear();
}
Expand Down Expand Up @@ -154,6 +155,7 @@ class MultiQPUQuantumPlatform : public cudaq::quantum_platform {
qpuSubType));
if (qpuSubType == "NvcfSimulatorQPU") {
platformQPUs.clear();
threadToQpuId.clear();
auto simName = getOpt(description, "backend");
if (simName.empty())
simName = "custatevec-fp32";
Expand Down Expand Up @@ -199,6 +201,7 @@ class MultiQPUQuantumPlatform : public cudaq::quantum_platform {
} else if (qpuSubType == "orca") {
auto urls = cudaq::split(getOpt(description, "url"), ',');
platformQPUs.clear();
threadToQpuId.clear();
for (std::size_t qId = 0; qId < urls.size(); ++qId) {
// Populate the information and add the QPUs
platformQPUs.emplace_back(cudaq::registry::get<cudaq::QPU>("orca"));
Expand Down Expand Up @@ -244,6 +247,7 @@ class MultiQPUQuantumPlatform : public cudaq::quantum_platform {
"receiving {}, expecting {}.",
sims.size(), urls.size()));
platformQPUs.clear();
threadToQpuId.clear();
for (std::size_t qId = 0; qId < urls.size(); ++qId) {
const auto simName = sims.size() == 1 ? sims.front() : sims[qId];
// Populate the information and add the QPUs
Expand Down
5 changes: 4 additions & 1 deletion unittests/Optimizer/QuakeSynthTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ TEST(QuakeSynthTests, checkVectorOfInt) {
kernel.h(aq);
kernel.z(aq);
kernel.h(q);
for (std::size_t i = 0; i < *q.constantSize(); ++i) {
// FIXME: This test never really tested the c_if in this loop. The call to
// constantSize just returned 0.
std::size_t unrollBy = q.constantSize().has_value() ? *q.constantSize() : 0;
for (std::size_t i = 0; i < unrollBy; ++i) {
kernel.c_if(hiddenBits[i], [&]() { kernel.x<cudaq::ctrl>(aq, q[i]); });
}
kernel.h(q);
Expand Down

0 comments on commit 1987897

Please sign in to comment.