Skip to content
Open
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
4 changes: 2 additions & 2 deletions frontend/test/lit/GraphDecomposition/TestAltDecomps.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func.func @circuit() -> !quantum.bit {
}

// CHECK-LABEL: y_to_ry
func.func @y_to_ry(%q0 : !quantum.bit) -> !quantum.bit attributes {target_gate="PauliY"} {
func.func @y_to_ry(%q0 : !quantum.bit) -> !quantum.bit attributes {target_gate="PauliY[][1]{}", resources = { operations = {"RY[f64][1]{}"=1, "GlobalPhase[][]{}"=1}}} {
%pi = arith.constant 3.14 : f64
%negpiby2 = arith.constant -1.57 : f64
%q1 = quantum.custom "RY"(%pi) %q0 : !quantum.bit
Expand All @@ -43,7 +43,7 @@ func.func @y_to_ry(%q0 : !quantum.bit) -> !quantum.bit attributes {target_gate="
}

// CHECK-LABEL: y_to_x_z
func.func @y_to_x_z(%q0 : !quantum.bit) -> !quantum.bit attributes {target_gate="PauliY"} {
func.func @y_to_x_z(%q0 : !quantum.bit) -> !quantum.bit attributes {target_gate="PauliY[][1]{}", resources = { operations = {"PauliX[][1]{}"=1, "PauliZ[][1]{}"=1}}} {
%q1 = quantum.custom "PauliX"() %q0 : !quantum.bit
%q2 = quantum.custom "PauliZ"() %q1 : !quantum.bit
return %q2 : !quantum.bit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ struct DecompositionGraph::Impl {
registerOp(op);
}

// Register all target gates
for (const auto &[op, _] : gateset.ops) {
registerOp(op);
}

// Register all rules
for (RuleId ruleId = 0; ruleId < rules.size(); ruleId++) {
const auto &rule = rules[ruleId];
Expand Down Expand Up @@ -322,8 +317,8 @@ void DecompositionGraph::showGraph() const

// Show target gateset
std::cerr << "Target Gateset:\n";
for (const auto &[op, cost] : impl->gateset.ops) {
std::cerr << " " << print_op(op) << " with cost " << cost << "\n";
for (const auto &[name, cost] : impl->gateset.ops) {
std::cerr << " " << name << " with cost " << cost << "\n";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ namespace DecompGraph::Core {
*/
struct OperatorNode {
std::string id;
std::string name; // name is required for gateset checking

bool adjoint{false};

// optional params, primarily for debug use
std::string name{""};
int numWires{-1};
int numParams{-1};
std::unordered_map<std::string, std::string> staticNamedArgs{};
Expand Down Expand Up @@ -92,15 +93,16 @@ struct OperatorNodeHash {
* @brief This represents the weighted target gateset for the graph decomposition problem.
*/
struct WeightedGateset {
// TODO: using ID here mandates that gatesets specify all legal IDs, rather than generic class
// like "PauliRot". This should be updated to work on generic names
std::unordered_map<OperatorNode, double, OperatorNodeHash> ops;
std::unordered_map<std::string, double> ops;

[[nodiscard]] bool contains(const OperatorNode &op) const { return ops.find(op) != ops.end(); }
[[nodiscard]] bool contains(const OperatorNode &op) const
{
return ops.find(op.name) != ops.end();
}

[[nodiscard]] double getCost(const OperatorNode &op) const
{
auto it = ops.find(op);
auto it = ops.find(op.name);
return it != ops.end() ? it->second : std::numeric_limits<double>::infinity();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "DGBuilder.hpp"
#include "DGSolver.hpp"
#include "DGTypes.hpp"
#include "DGUtils.hpp"
#include "DecompUtils.hpp"

#define DEBUG_TYPE "graph-decomposition"
Expand Down Expand Up @@ -139,6 +140,7 @@ struct GraphDecompositionPass : public impl::GraphDecompositionPassBase<GraphDec
std::move(altDecomps));
DecompositionSolver solver(graph);
auto solution = solver.solve();
LLVM_DEBUG(showSolution(solution););

///////////////////////////
// Step 3: Convert python-decompositions from reference to value semantics and run
Expand Down Expand Up @@ -220,7 +222,7 @@ struct GraphDecompositionPass : public impl::GraphDecompositionPassBase<GraphDec
cost.consume_back(": f64");
cost = cost.trim();

bool success = to_float(cost, targetGateSet.ops[OperatorNode{opName.str()}]);
bool success = to_float(cost, targetGateSet.ops[opName.str()]);

if (!success) {
return failure();
Expand Down
53 changes: 29 additions & 24 deletions mlir/unittests/DecompGraphSolver/Test_DecompGraphCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,38 @@ using namespace DecompGraph::Core;

TEST_CASE("Test OperatorNode construction", "[DecompGraph::Core]")
{
const OperatorNode h{"Hadamard[][1]{}"};
const OperatorNode cnot{"CNOT[][2]{}"};
const OperatorNode rx{"RX[f64][1]{}"};
const OperatorNode rz{"RZ[f64][1]{}"};
const OperatorNode h{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode cnot{"CNOT[][2]{}", "CNOT"};
const OperatorNode rx{"RX[f64][1]{}", "RX"};
const OperatorNode rz{"RZ[f64][1]{}", "RZ"};

REQUIRE(h.id == "Hadamard[][1]{}");
REQUIRE(cnot.id == "CNOT[][2]{}");
REQUIRE(rx.id == "RX[f64][1]{}");
REQUIRE(rz.id == "RZ[f64][1]{}");

REQUIRE(h.name == "Hadamard");
REQUIRE(cnot.name == "CNOT");
REQUIRE(rx.name == "RX");
REQUIRE(rz.name == "RZ");
}

TEST_CASE("Test OperatorNode equality operator", "[DecompGraph::Core]")
{
const OperatorNode h1{"Hadamard[][1]{}"};
const OperatorNode h2{"Hadamard[][1]{}"};
const OperatorNode cnot{"CNOT[][2]{}"};
const OperatorNode h1{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode h2{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode cnot{"CNOT[][2]{}", "CNOT"};

REQUIRE(h1 == h2);
REQUIRE(h1 != cnot);
}

TEST_CASE("Test OperatorNodeHash", "[DecompGraph::Core]")
{
const OperatorNode h1{"Hadamard[][1]{}"};
const OperatorNode h2{"Hadamard[][1]{}"};
const OperatorNode h3{"Hadamard[][1]{}"};
const OperatorNode cnot{"CNOT[][2]{}"};
const OperatorNode h1{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode h2{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode h3{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode cnot{"CNOT[][2]{}", "CNOT"};

const OperatorNodeHash hashFunc;
REQUIRE(hashFunc(h1) == hashFunc(h2));
Expand All @@ -63,9 +68,9 @@ TEST_CASE("Test OperatorNodeHash", "[DecompGraph::Core]")
TEST_CASE("Test OperatorNode in unordered_map", "[DecompGraph::Core]")
{
std::unordered_map<OperatorNode, double, OperatorNodeHash> opMap;
const OperatorNode h{"Hadamard[][1]{}"};
const OperatorNode cnot{"CNOT[][2]{}"};
const OperatorNode rx{"RX[f64][1]{}"};
const OperatorNode h{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode cnot{"CNOT[][2]{}", "CNOT"};
const OperatorNode rx{"RX[f64][1]{}", "RX"};

opMap[h] = 1.0;
opMap[cnot] = 2.0;
Expand All @@ -77,9 +82,9 @@ TEST_CASE("Test OperatorNode in unordered_map", "[DecompGraph::Core]")

TEST_CASE("Test RuleNode construction", "[DecompGraph::Core]")
{
const OperatorNode h{"Hadamard[][1]{}"};
const OperatorNode rx{"RX[f64][1]{}"};
const OperatorNode rz{"RZ[f64][1]{}"};
const OperatorNode h{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode rx{"RX[f64][1]{}", "RX"};
const OperatorNode rz{"RZ[f64][1]{}", "RZ"};

const RuleNode h_to_rz_rx_rz{"h_to_rz_rx_rz", h, {{rz, 2}, {rx, 1}}};
REQUIRE(h_to_rz_rx_rz.name == "h_to_rz_rx_rz");
Expand All @@ -93,11 +98,11 @@ TEST_CASE("Test RuleNode construction", "[DecompGraph::Core]")

TEST_CASE("Test WeightedGateset construction and contains", "[DecompGraph::Core]")
{
const OperatorNode h{"Hadamard[][1]{}"};
const OperatorNode cnot{"CNOT[][2]{}"};
const OperatorNode rx{"RX[f64][1]{}"};
const OperatorNode h{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode cnot{"CNOT[][2]{}", "CNOT"};
const OperatorNode rx{"RX[f64][1]{}", "RX"};

const WeightedGateset gateset{{{h, 1.0}, {cnot, 2.0}}};
const WeightedGateset gateset{{{h.name, 1.0}, {cnot.name, 2.0}}};

REQUIRE(gateset.contains(h));
REQUIRE(gateset.contains(cnot));
Expand All @@ -109,9 +114,9 @@ TEST_CASE("Test WeightedGateset construction and contains", "[DecompGraph::Core]

TEST_CASE("Test ChosenDecompRule construction", "[DecompGraph::Core]")
{
const OperatorNode h{"Hadamard[][1]{}"};
const OperatorNode rx{"RX[f64][1]{}"};
const OperatorNode rz{"RZ[f64][1]{}"};
const OperatorNode h{"Hadamard[][1]{}", "Hadamard"};
const OperatorNode rx{"RX[f64][1]{}", "RX"};
const OperatorNode rz{"RZ[f64][1]{}", "RZ"};

const RuleTerm term1{rz, 2};
const RuleTerm term2{rx, 1};
Expand Down
Loading
Loading