Skip to content

Commit ef174bf

Browse files
resolve PR comments
1 parent aa16ead commit ef174bf

File tree

4 files changed

+38
-44
lines changed

4 files changed

+38
-44
lines changed

frontend/catalyst/compiler.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,12 @@ def to_mlir_opt(*args, stdin=None, options: Optional[CompileOptions] = None):
374374

375375
opts = _options_to_cli_flags(options)
376376
raw_result = _quantum_opt(*opts, *args, stdin=stdin)
377-
regex_search_for_json = re.search(r"\{[a-zA-Z0-9_\":\{\},\n]+\}", raw_result)
377+
regex_search_for_json = re.search(r"\{.*?\}", raw_result, re.DOTALL)
378+
379+
# No ppm_specs json is found
380+
if regex_search_for_json is None:
381+
return raw_result
382+
378383
raw_result = raw_result.replace(regex_search_for_json.group(0), "")
379384
return raw_result
380385

@@ -388,12 +393,7 @@ def to_ppm_spec(*args, stdin=None, options: Optional[CompileOptions] = None):
388393
opts = _options_to_cli_flags(options)
389394

390395
raw_json_format = _quantum_opt(*opts, *args, stdin=stdin)
391-
regex_search_for_json = re.search(r"\{[a-zA-Z0-9_\":\{\},\n]+\}", raw_json_format)
392-
393-
# No ppm_specs json is found
394-
if regex_search_for_json is None:
395-
return raw_json_format
396-
396+
regex_search_for_json = re.search(r"\{.*?\}", raw_json_format, re.DOTALL)
397397
json_ppm_specs = regex_search_for_json.group(0)
398398
json_ppm_specs = json_ppm_specs.replace(",\n}", "\n}")
399399
json_ppm_specs = json.loads(json_ppm_specs)

mlir/include/QEC/Transforms/Passes.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ def CliffordTToPPMPass : Pass<"ppm_compilation"> {
124124
def CountPPMSpecsPass : Pass<"ppm_specs"> {
125125
let summary = "Count specs in Pauli Product Measurement operations.";
126126

127-
let dependentDialects = [
128-
"catalyst::qec::QECDialect",
129-
];
130-
131127
let constructor = "catalyst::createCountPPMSpecsPass()";
132128
}
133129

mlir/lib/QEC/Transforms/CMakeLists.txt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,27 @@ set(DEPENDS
3232

3333
add_mlir_library(${LIBRARY_NAME} STATIC ${SRC} LINK_LIBS PRIVATE ${LIBS} DEPENDS ${DEPENDS})
3434
target_compile_features(${LIBRARY_NAME} PUBLIC cxx_std_20)
35+
36+
include(FetchContent)
37+
FetchContent_Declare(json
38+
URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
39+
URL_HASH SHA256=d6c65aca6b1ed68e7a182f4757257b107ae403032760ed6ef121c9d55e81757d
40+
DOWNLOAD_EXTRACT_TIMESTAMP true
41+
SYSTEM
42+
)
43+
FetchContent_MakeAvailable(json)
44+
target_link_libraries(${LIBRARY_NAME}
45+
PRIVATE nlohmann_json::nlohmann_json
46+
)
47+
48+
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
49+
set_source_files_properties(
50+
CountPPMSpecs.cpp
51+
COMPILE_FLAGS "-Wno-covered-switch-default"
52+
)
53+
endif()
54+
3555
target_include_directories(${LIBRARY_NAME} PUBLIC
3656
.
3757
${PROJECT_SOURCE_DIR}/include
38-
${CMAKE_BINARY_DIR}/include)
58+
${CMAKE_BINARY_DIR}/include)

mlir/lib/QEC/Transforms/CountPPMSpecs.cpp

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@
1414

1515
#define DEBUG_TYPE "ppm_specs"
1616

17-
#include "mlir/Analysis/SliceAnalysis.h"
17+
#include <algorithm>
18+
#include <string>
19+
20+
#include <nlohmann/json.hpp>
21+
1822
#include "mlir/IR/PatternMatch.h"
1923
#include "mlir/Pass/Pass.h"
20-
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
21-
2224

2325
#include "QEC/IR/QECDialect.h"
24-
#include "QEC/Transforms/Patterns.h"
25-
#include "Quantum/IR/QuantumOps.h"
26-
#include "QEC/Utils/PauliStringWrapper.h"
27-
#include <algorithm>
28-
#include <string>
2926

3027
using namespace llvm;
3128
using namespace mlir;
3229
using namespace catalyst;
3330
using namespace catalyst::qec;
31+
using json = nlohmann::json;
3432

3533
namespace catalyst {
3634
namespace qec {
@@ -39,7 +37,6 @@ namespace qec {
3937
#define GEN_PASS_DECL_COUNTPPMSPECSPASS
4038
#include "QEC/Transforms/Passes.h.inc"
4139

42-
4340
struct CountPPMSpecsPass : public impl::CountPPMSpecsPassBase<CountPPMSpecsPass> {
4441
using CountPPMSpecsPassBase::CountPPMSpecsPassBase;
4542

@@ -55,11 +52,6 @@ struct CountPPMSpecsPass : public impl::CountPPMSpecsPassBase<CountPPMSpecsPass>
5552
// Skip top-level container ops if desired
5653
if (isa<ModuleOp>(op)) return;
5754

58-
// TODO: Remove debug in future
59-
// llvm::outs()<<"\n-----------------------------MLIR------------------------------\n";
60-
// op->print(llvm::outs());
61-
// llvm::outs()<<"\n-----------------------------MLIR------------------------------\n";
62-
6355
StringRef gate_name = op->getName().getStringRef();
6456

6557
if (gate_name == "quantum.alloc") {
@@ -69,7 +61,7 @@ struct CountPPMSpecsPass : public impl::CountPPMSpecsPassBase<CountPPMSpecsPass>
6961
}
7062

7163
if (gate_name == "qec.ppm") {
72-
PPM_Specs["num_of_ppm"] = PPM_Specs["num_of_ppm"] + 1;
64+
PPM_Specs["num_of_ppm"]++;
7365
}
7466

7567
if (gate_name == "qec.ppr") {
@@ -86,29 +78,15 @@ struct CountPPMSpecsPass : public impl::CountPPMSpecsPassBase<CountPPMSpecsPass>
8678
PPM_Specs[max_weight_pi_key] = static_cast<int>(pauli_product_attr.size());
8779
}
8880
else {
89-
PPM_Specs[num_pi_key] = PPM_Specs[num_pi_key] + 1;
81+
PPM_Specs[num_pi_key]++;
9082
PPM_Specs[max_weight_pi_key] = std::max(PPM_Specs[max_weight_pi_key], static_cast<int>(pauli_product_attr.size()));
9183
}
9284
}
9385
}
94-
// TODO: Implement depth using slicing
95-
// mlir::SetVector <Operation *> backwardSlice;
96-
// getBackwardSlice(op, &backwardSlice);
97-
// llvm::outs()<<"\n-----------------------------SLICE-----------------------------\n";
98-
// llvm::outs()<<"Backward slicing\n";
99-
// for (Operation *o : backwardSlice) {
100-
// if (o->getName().getStringRef() == "quantum.extract") {
101-
// llvm::outs() << *o << "\n";
102-
// }
103-
// }
104-
// llvm::outs()<<"\n-----------------------------SLICE------------------------------\n";
10586
});
10687

107-
llvm::outs() << "{\n";
108-
for (const auto &entry : PPM_Specs) {
109-
llvm::outs() << '"' << entry.first << '"' << ":" << entry.second << ",\n";
110-
}
111-
llvm::outs() << "}\n";
88+
json PPM_Specs_Json = PPM_Specs;
89+
llvm::outs() << PPM_Specs_Json.dump(4) << "\n";
11290
return;
11391
}
11492

0 commit comments

Comments
 (0)