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

[braket] Python tests #2401

Merged
merged 19 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
9 changes: 9 additions & 0 deletions lib/Optimizer/CodeGen/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@

using namespace mlir;

static SmallVector<std::string> z_disabledPatterns = {"R1ToU3"};

static void addAnyonPPipeline(OpPassManager &pm) {
using namespace cudaq::opt;
std::string basis[] = {
"h", "s", "t", "rx", "ry", "rz", "x", "y", "z", "z(1)",
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -31,6 +34,7 @@ static void addAnyonCPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -42,6 +46,7 @@ static void addOQCPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -52,6 +57,7 @@ static void addQuantinuumPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -63,6 +69,7 @@ static void addIQMPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -74,6 +81,7 @@ static void addIonQPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand All @@ -84,6 +92,7 @@ static void addFermioniqPipeline(OpPassManager &pm) {
};
BasisConversionPassOptions options;
options.basis = basis;
options.disabledPatterns = z_disabledPatterns;
pm.addPass(createBasisConversionPass(options));
}

Expand Down
9 changes: 8 additions & 1 deletion lib/Optimizer/CodeGen/TranslateToOpenQASM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cudaq/Frontend/nvqpp/AttributeNames.h"
#include "cudaq/Optimizer/CodeGen/Emitter.h"
#include "cudaq/Optimizer/CodeGen/OpenQASMEmitter.h"
#include "cudaq/Optimizer/Dialect/CC/CCOps.h"
#include "cudaq/Optimizer/Dialect/CC/CCTypes.h"
#include "cudaq/Optimizer/Dialect/Quake/QuakeOps.h"
#include "llvm/ADT/PostOrderIterator.h"
Expand Down Expand Up @@ -39,7 +40,8 @@ static LogicalResult translateOperatorName(quake::OperatorInterface optor,
.Case("rx", "crx")
.Case("ry", "cry")
.Case("rz", "crz")
.Default("");
.Case("swap", "cswap")
.Default(qkeName);
} else if (optor.getControls().size() == 2) {
name = StringSwitch<StringRef>(qkeName).Case("x", "ccx").Default("");
}
Expand Down Expand Up @@ -345,6 +347,11 @@ static LogicalResult emitOperation(Emitter &emitter, Operation &op) {
.Case<DeallocOp>([&](auto op) { return success(); })
.Case<func::ReturnOp>([&](auto op) { return success(); })
.Case<arith::ConstantOp>([&](auto op) { return success(); })
.Case<cudaq::cc::AllocaOp>([&](auto op) { return success(); })
.Case<cudaq::cc::StoreOp>([&](auto op) { return success(); })
.Case<cudaq::cc::CastOp>([&](auto op) { return success(); })
.Case<cudaq::cc::ComputePtrOp>([&](auto op) { return success(); })
.Case<quake::DiscriminateOp>([&](auto op) { return success(); })
.Default([&](Operation *) -> LogicalResult {
if (op.getName().getDialectNamespace().equals("llvm"))
return success();
Expand Down
21 changes: 21 additions & 0 deletions lib/Optimizer/Transforms/DecompositionPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ struct R1ToRz : public OpRewritePattern<quake::R1Op> {
}
};

// Naive mapping of R1 to U3
// quake.r1(λ) [control] target
// ───────────────────────────────────
// quake.u3(0, 0, λ) [control] target
struct R1ToU3 : public OpRewritePattern<quake::R1Op> {
using OpRewritePattern<quake::R1Op>::OpRewritePattern;

void initialize() { setDebugName("R1ToU3"); }

LogicalResult matchAndRewrite(quake::R1Op r1Op,
PatternRewriter &rewriter) const override {
Location loc = r1Op->getLoc();
Value zero = createConstant(loc, 0.0, rewriter.getF64Type(), rewriter);
std::array<Value, 3> parameters = {zero, zero, r1Op.getParameters()[0]};
rewriter.replaceOpWithNewOp<quake::U3Op>(
r1Op, r1Op.isAdj(), parameters, r1Op.getControls(), r1Op.getTargets());
return success();
}
};

// quake.swap a, b
// ───────────────────────────────────
// quake.cnot b, a;
Expand Down Expand Up @@ -1554,6 +1574,7 @@ void cudaq::populateWithAllDecompositionPatterns(RewritePatternSet &patterns) {
CR1ToCX,
R1ToPhasedRx,
R1ToRz,
R1ToU3,
// RxOp patterns
CRxToCX,
RxToPhasedRx,
Expand Down
1 change: 0 additions & 1 deletion python/runtime/utils/PyRemoteRESTQPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class PyRemoteRESTQPU : public cudaq::BaseRemoteRESTQPU {
pm.addPass(mlir::createCanonicalizerPass());
pm.addPass(cudaq::opt::createApplyOpSpecializationPass());
pm.addPass(createInlinerPass());
pm.addPass(cudaq::opt::createExpandMeasurementsPass());
pm.addPass(createCanonicalizerPass());
pm.addPass(createCSEPass());
if (failed(pm.run(cloned)))
Expand Down
2 changes: 1 addition & 1 deletion python/tests/backends/test_QuEra.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def startUpMockServer():
p.terminate()


@pytest.mark.skip(reason="Braket credentials required")
@pytest.mark.skip(reason="Amazon Braket credentials required")
def test_JSON_payload():
'''
Test based on https://docs.aws.amazon.com/braket/latest/developerguide/braket-quera-submitting-analog-program-aquila.html
Expand Down
Loading
Loading