Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning], with the exception that minor rel

### Added

- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402], [#1428], [#1430], [#1436], [#1443], [#1446]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**])
- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402], [#1428], [#1430], [#1436], [#1443], [#1446], [#1465]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**], [**@li-mingbao**])

### Changed

Expand Down Expand Up @@ -314,6 +314,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool

<!-- PR links -->

[#1465]: https://github.com/munich-quantum-toolkit/core/pull/1465
[#1458]: https://github.com/munich-quantum-toolkit/core/pull/1458
[#1453]: https://github.com/munich-quantum-toolkit/core/pull/1453
[#1447]: https://github.com/munich-quantum-toolkit/core/pull/1447
Expand Down
36 changes: 22 additions & 14 deletions mlir/include/mlir/Dialect/Utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,41 @@

#include <mlir/Dialect/Arith/IR/Arith.h>
#include <mlir/IR/Builders.h>
#include <mlir/IR/OperationSupport.h>
#include <mlir/IR/Location.h>
#include <mlir/IR/Value.h>
#include <variant>

namespace mlir::utils {

constexpr auto TOLERANCE = 1e-15;

inline Value constantFromScalar(OpBuilder& builder, Location loc, double v) {
return arith::ConstantOp::create(builder, loc, builder.getF64FloatAttr(v));
}

inline Value constantFromScalar(OpBuilder& builder, Location loc, int64_t v) {
return arith::ConstantOp::create(builder, loc, builder.getIndexAttr(v));
}

inline Value constantFromScalar(OpBuilder& builder, Location loc, bool v) {
return arith::ConstantOp::create(builder, loc, builder.getBoolAttr(v));
}

/**
* @brief Convert a variant parameter (double or Value) to a Value
* @brief Convert a variant parameter (T or Value) to a Value.
*
* @param builder The operation builder.
* @param state The operation state.
* @param parameter The parameter as a variant (double or Value).
* @param loc The location of the operation.
* @param parameter The parameter as a variant (T or Value).
* @return Value The parameter as a Value.
*/
[[nodiscard]] inline Value
variantToValue(OpBuilder& builder, const OperationState& state,
const std::variant<double, Value>& parameter) {
Value operand;
if (std::holds_alternative<double>(parameter)) {
operand = builder.create<arith::ConstantOp>(
state.location, builder.getF64FloatAttr(std::get<double>(parameter)));
} else {
operand = std::get<Value>(parameter);
template <typename T>
[[nodiscard]] Value variantToValue(OpBuilder& builder, Location loc,
const std::variant<T, Value>& parameter) {
if (std::holds_alternative<Value>(parameter)) {
return std::get<Value>(parameter);
}
return operand;
return constantFromScalar(builder, loc, std::get<T>(parameter));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void GPhaseOp::build(OpBuilder& builder, OperationState& state,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/POp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void POp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/QC/IR/Operations/StandardGates/ROp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace mlir::utils;
void ROp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& phi) {
auto thetaOperand = variantToValue(builder, state, theta);
auto phiOperand = variantToValue(builder, state, phi);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto phiOperand = variantToValue(builder, state.location, phi);
build(builder, state, qubitIn, thetaOperand, phiOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RXOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RXXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RXXOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RYOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RYOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RYYOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RYYOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RZOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RZOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RZXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RZXOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QC/IR/Operations/StandardGates/RZZOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ using namespace mlir::utils;

void RZZOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/QC/IR/Operations/StandardGates/U2Op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using namespace mlir::utils;
void U2Op::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& phi,
const std::variant<double, Value>& lambda) {
auto phiOperand = variantToValue(builder, state, phi);
auto lambdaOperand = variantToValue(builder, state, lambda);
auto phiOperand = variantToValue(builder, state.location, phi);
auto lambdaOperand = variantToValue(builder, state.location, lambda);
build(builder, state, qubitIn, phiOperand, lambdaOperand);
}
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/QC/IR/Operations/StandardGates/UOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void UOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& phi,
const std::variant<double, Value>& lambda) {
auto thetaOperand = variantToValue(builder, state, theta);
auto phiOperand = variantToValue(builder, state, phi);
auto lambdaOperand = variantToValue(builder, state, lambda);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto phiOperand = variantToValue(builder, state.location, phi);
auto lambdaOperand = variantToValue(builder, state.location, lambda);
build(builder, state, qubitIn, thetaOperand, phiOperand, lambdaOperand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void XXMinusYYOp::build(OpBuilder& builder, OperationState& state,
Value qubit0In, Value qubit1In,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& beta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto betaOperand = variantToValue(builder, state, beta);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto betaOperand = variantToValue(builder, state.location, beta);
build(builder, state, qubit0In, qubit1In, thetaOperand, betaOperand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void XXPlusYYOp::build(OpBuilder& builder, OperationState& state,
Value qubit0In, Value qubit1In,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& beta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto betaOperand = variantToValue(builder, state, beta);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto betaOperand = variantToValue(builder, state.location, beta);
build(builder, state, qubit0In, qubit1In, thetaOperand, betaOperand);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct RemoveTrivialGPhase final : OpRewritePattern<GPhaseOp> {

void GPhaseOp::build(OpBuilder& builder, OperationState& state,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/POp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialP final : OpRewritePattern<POp> {

void POp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/QCO/IR/Operations/StandardGates/ROp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ struct ReplaceRWithRY final : OpRewritePattern<ROp> {
void ROp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& phi) {
auto thetaOperand = variantToValue(builder, state, theta);
auto phiOperand = variantToValue(builder, state, phi);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto phiOperand = variantToValue(builder, state.location, phi);
build(builder, state, qubitIn, thetaOperand, phiOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRX final : OpRewritePattern<RXOp> {

void RXOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RXXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRXX final : OpRewritePattern<RXXOp> {

void RXXOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RYOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRY final : OpRewritePattern<RYOp> {

void RYOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RYYOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRYY final : OpRewritePattern<RYYOp> {

void RYYOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RZOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRZ final : OpRewritePattern<RZOp> {

void RZOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubitIn, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RZXOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRZX final : OpRewritePattern<RZXOp> {

void RZXOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}

Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/QCO/IR/Operations/StandardGates/RZZOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct RemoveTrivialRZZ final : OpRewritePattern<RZZOp> {

void RZZOp::build(OpBuilder& builder, OperationState& state, Value qubit0In,
Value qubit1In, const std::variant<double, Value>& theta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto thetaOperand = variantToValue(builder, state.location, theta);
build(builder, state, qubit0In, qubit1In, thetaOperand);
}

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/QCO/IR/Operations/StandardGates/U2Op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ struct ReplaceU2WithRY final : OpRewritePattern<U2Op> {
void U2Op::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& phi,
const std::variant<double, Value>& lambda) {
auto phiOperand = variantToValue(builder, state, phi);
auto lambdaOperand = variantToValue(builder, state, lambda);
auto phiOperand = variantToValue(builder, state.location, phi);
auto lambdaOperand = variantToValue(builder, state.location, lambda);
build(builder, state, qubitIn, phiOperand, lambdaOperand);
}

Expand Down
6 changes: 3 additions & 3 deletions mlir/lib/Dialect/QCO/IR/Operations/StandardGates/UOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ void UOp::build(OpBuilder& builder, OperationState& state, Value qubitIn,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& phi,
const std::variant<double, Value>& lambda) {
auto thetaOperand = variantToValue(builder, state, theta);
auto phiOperand = variantToValue(builder, state, phi);
auto lambdaOperand = variantToValue(builder, state, lambda);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto phiOperand = variantToValue(builder, state.location, phi);
auto lambdaOperand = variantToValue(builder, state.location, lambda);
build(builder, state, qubitIn, thetaOperand, phiOperand, lambdaOperand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void XXMinusYYOp::build(OpBuilder& builder, OperationState& state,
Value qubit0In, Value qubit1In,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& beta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto betaOperand = variantToValue(builder, state, beta);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto betaOperand = variantToValue(builder, state.location, beta);
build(builder, state, qubit0In, qubit1In, thetaOperand, betaOperand);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ void XXPlusYYOp::build(OpBuilder& builder, OperationState& state,
Value qubit0In, Value qubit1In,
const std::variant<double, Value>& theta,
const std::variant<double, Value>& beta) {
auto thetaOperand = variantToValue(builder, state, theta);
auto betaOperand = variantToValue(builder, state, beta);
auto thetaOperand = variantToValue(builder, state.location, theta);
auto betaOperand = variantToValue(builder, state.location, beta);
build(builder, state, qubit0In, qubit1In, thetaOperand, betaOperand);
}

Expand Down
Loading