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

[CircuitCheck] Handle raw index in ExtractRefOp #168

Merged
merged 1 commit into from
May 17, 2023
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
29 changes: 17 additions & 12 deletions utils/CircuitCheck/UnitaryBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ LogicalResult UnitaryBuilder::build(func::FuncOp func) {
//===----------------------------------------------------------------------===//

WalkResult UnitaryBuilder::visitExtractOp(quake::ExtractRefOp op) {
auto veq = op.getVeq();
auto qubits = qubitMap[veq];
auto index = getValueAsInt(op.getIndex());
if (!index && *index < 0)
Value veq = op.getVeq();
ArrayRef<unsigned> qubits = qubitMap[veq];
size_t index = 0;
// We need to check whether the index is a "raw" index or not.
if (op.hasConstantIndex())
index = op.getRawIndex();
else if (failed(getValueAsInt(op.getIndex(), index)))
return WalkResult::interrupt();
auto [entry, _] = qubitMap.try_emplace(op.getResult());
entry->second.push_back(qubits[*index]);
entry->second.push_back(qubits[index]);
return WalkResult::advance();
}

Expand All @@ -75,7 +78,7 @@ WalkResult UnitaryBuilder::allocateQubits(Value value) {
if (!success)
return WalkResult::interrupt();
auto &qubits = entry->second;
if (auto veq = value.getType().dyn_cast<quake::VeqType>()) {
if (auto veq = dyn_cast<quake::VeqType>(value.getType())) {
if (!veq.hasSpecifiedSize())
return WalkResult::interrupt();
qubits.resize(veq.getSize());
Expand All @@ -87,12 +90,14 @@ WalkResult UnitaryBuilder::allocateQubits(Value value) {
return WalkResult::advance();
}

std::optional<int64_t> UnitaryBuilder::getValueAsInt(Value value) {
if (auto constOp =
dyn_cast_if_present<arith::ConstantOp>(value.getDefiningOp()))
if (auto index = dyn_cast<IntegerAttr>(constOp.getValue()))
return index.getInt();
return std::nullopt;
LogicalResult UnitaryBuilder::getValueAsInt(Value value, size_t &result) {
if (auto op =
dyn_cast_if_present<arith::ConstantIntOp>(value.getDefiningOp()))
if (auto index = dyn_cast<IntegerAttr>(op.getValue())) {
result = index.getInt();
return success();
}
return failure();
}

//===----------------------------------------------------------------------===//
Expand Down
4 changes: 2 additions & 2 deletions utils/CircuitCheck/UnitaryBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class UnitaryBuilder {

mlir::WalkResult allocateQubits(mlir::Value value);

std::optional<int64_t> getValueAsInt(mlir::Value value);

//===--------------------------------------------------------------------===//
// Helpers
//===--------------------------------------------------------------------===//

mlir::LogicalResult getValueAsInt(mlir::Value value, size_t &result);

unsigned getNextQubit() { return std::log2(matrix.rows()); }

mlir::LogicalResult getQubits(mlir::ValueRange values,
Expand Down