Skip to content

Commit

Permalink
refactor(assertion-tools): ♻️ Rename "commutativity" to "commutation"
Browse files Browse the repository at this point in the history
  • Loading branch information
DRovara committed Nov 20, 2024
1 parent 9c14b24 commit a57ec90
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
16 changes: 8 additions & 8 deletions include/common/parsing/AssertionTools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

#include <memory>

#define COMMUTATIVITY_RULE_GENERAL(name, expression) \
COMMUTATIVITY_RULE_TEMPLATE(Assertion, name, expression)
#define COMMUTATION_RULE_GENERAL(name, expression) \
COMMUTATION_RULE_TEMPLATE(Assertion, name, expression)

#define COMMUTATIVITY_RULE_ENT(name, expression) \
COMMUTATIVITY_RULE_TEMPLATE(EntanglementAssertion, name, expression)
#define COMMUTATION_RULE_ENT(name, expression) \
COMMUTATION_RULE_TEMPLATE(EntanglementAssertion, name, expression)

#define COMMUTATIVITY_RULE_SUP(name, expression) \
COMMUTATIVITY_RULE_TEMPLATE(SuperpositionAssertion, name, expression)
#define COMMUTATION_RULE_SUP(name, expression) \
COMMUTATION_RULE_TEMPLATE(SuperpositionAssertion, name, expression)

#define COMMUTATIVITY_RULE_TEMPLATE(type, name, expression) \
#define COMMUTATION_RULE_TEMPLATE(type, name, expression) \
const auto name = [](const type* assertion, \
const std::string& instructionName, \
const std::vector<std::string>& arguments) { \
Expand All @@ -24,7 +24,7 @@
return expression; \
}

enum class CommutativityResult {
enum class CommutationResult {

Check warning on line 27 in include/common/parsing/AssertionTools.hpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

include/common/parsing/AssertionTools.hpp:27:12 [performance-enum-size]

enum 'CommutationResult' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to reduce its size
Commutes,
DoesNotCommute,
Unknown,
Expand Down
55 changes: 27 additions & 28 deletions src/common/parsing/AssertionTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,70 @@
#include <vector>

#define YES(expression) \
(expression) ? CommutativityResult::Commutes : CommutativityResult::Unknown;
(expression) ? CommutationResult::Commutes : CommutationResult::Unknown;
#define NO(expression) \
(expression) ? CommutativityResult::DoesNotCommute \
: CommutativityResult::Unknown;
(expression) ? CommutationResult::DoesNotCommute : CommutationResult::Unknown;

//------------------------------------------------------------------------------
// General rules
//------------------------------------------------------------------------------

// Barrier instructions will not remove or create entanglement or superposition
static COMMUTATIVITY_RULE_GENERAL(BARRIER, YES(instructionName == "barrier"));
static COMMUTATION_RULE_GENERAL(BARRIER, YES(instructionName == "barrier"));

//------------------------------------------------------------------------------
// Entanglement rules
//------------------------------------------------------------------------------

// 1-qubit gates will not remove or create entanglement
static COMMUTATIVITY_RULE_ENT(TWO_OR_MORE_TARGETS, YES(arguments.size() < 2));
static COMMUTATION_RULE_ENT(TWO_OR_MORE_TARGETS, YES(arguments.size() < 2));

//------------------------------------------------------------------------------
// Superposition rules
//------------------------------------------------------------------------------

// Pauli-Instructions will not remove or create superposition
static COMMUTATIVITY_RULE_SUP(PAULI_INVARIANT, YES(instructionName == "x" ||
instructionName == "y" ||
instructionName == "z"));
static COMMUTATION_RULE_SUP(PAULI_INVARIANT, YES(instructionName == "x" ||
instructionName == "y" ||
instructionName == "z"));

// `S` and `T` gates will not remove or create superposition
static COMMUTATIVITY_RULE_SUP(OTHER_1Q_GATE_INVARIANTS,
YES(instructionName == "s" ||
instructionName == "t" ||
instructionName == "sdg" ||
instructionName == "tdg"));
static COMMUTATION_RULE_SUP(OTHER_1Q_GATE_INVARIANTS,
YES(instructionName == "s" ||
instructionName == "t" ||
instructionName == "sdg" ||
instructionName == "tdg"));

//------------------------------------------------------------------------------

static const std::vector<std::function<CommutativityResult(
static const std::vector<std::function<CommutationResult(
const Assertion*, const std::string&, const std::vector<std::string>&)>>
GENERAL_COMMUTATIVITY_RULES = {
GENERAL_COMMUTATION_RULES = {
BARRIER,
};

static const std::vector<std::function<CommutativityResult(
static const std::vector<std::function<CommutationResult(
const EntanglementAssertion*, const std::string&,
const std::vector<std::string>&)>>
ENTANGLEMENT_COMMUTATIVITY_RULES = {
ENTANGLEMENT_COMMUTATION_RULES = {
TWO_OR_MORE_TARGETS,
};

static const std::vector<std::function<CommutativityResult(
static const std::vector<std::function<CommutationResult(
const SuperpositionAssertion*, const std::string&,
const std::vector<std::string>&)>>
SUPERPOSITION_COMMUTATIVITY_RULES = {
SUPERPOSITION_COMMUTATION_RULES = {
PAULI_INVARIANT,
OTHER_1Q_GATE_INVARIANTS,
};

bool doesCommuteEnt(const EntanglementAssertion* assertion,
const std::string& instructionName,
const std::vector<std::string>& targets) {
for (const auto& rule : ENTANGLEMENT_COMMUTATIVITY_RULES) {
for (const auto& rule : ENTANGLEMENT_COMMUTATION_RULES) {
const auto result = rule(assertion, instructionName, targets);
if (result != CommutativityResult::Unknown) {
return result == CommutativityResult::Commutes;
if (result != CommutationResult::Unknown) {
return result == CommutationResult::Commutes;
}
}
return false;
Expand All @@ -84,10 +83,10 @@ bool doesCommuteEnt(const EntanglementAssertion* assertion,
bool doesCommuteSup(const SuperpositionAssertion* assertion,
const std::string& instructionName,
const std::vector<std::string>& targets) {
for (const auto& rule : SUPERPOSITION_COMMUTATIVITY_RULES) {
for (const auto& rule : SUPERPOSITION_COMMUTATION_RULES) {
const auto result = rule(assertion, instructionName, targets);
if (result != CommutativityResult::Unknown) {
return result == CommutativityResult::Commutes;
if (result != CommutationResult::Unknown) {
return result == CommutationResult::Commutes;
}
}
return false;
Expand All @@ -97,10 +96,10 @@ bool doesCommute(const std::unique_ptr<Assertion>& assertion,
const std::string& instruction) {
const auto targets = parseParameters(instruction);
const auto instructionName = splitString(trim(instruction), ' ')[0];
for (const auto& rule : GENERAL_COMMUTATIVITY_RULES) {
for (const auto& rule : GENERAL_COMMUTATION_RULES) {
const auto result = rule(assertion.get(), instructionName, targets);
if (result != CommutativityResult::Unknown) {
return result == CommutativityResult::Commutes;
if (result != CommutationResult::Unknown) {
return result == CommutationResult::Commutes;
}
}

Expand Down

0 comments on commit a57ec90

Please sign in to comment.