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

Refactor handling of gate matrices and inverses #752

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Joshy-R
Copy link

@Joshy-R Joshy-R commented Nov 14, 2024

Description

These commits change how matrices are created from an OpType that reduces the number of switch statements necessary.
The downside of this approach is that some functions can be called with arguments that do not affect the returned value. As the user never calls these functions, I see this as acceptable.

I also removed the CX_MAT and CZ_MAT, as they are only used in tests and do not occur in any other repository.

Fixes #484

Checklist:

  • The pull request only contains commits that are related to it.
  • I have added appropriate tests and documentation.
  • I have made sure that all CI jobs on GitHub pass.
  • The pull request introduces no new warnings and follows the project's style guidelines.

I have not squashed the commits to make them easier to review, but I can squash them if desired.

@Joshy-R Joshy-R force-pushed the refactor_handling_of_gate_matrices_and_inverses branch from 8c9a224 to 0c22ab7 Compare November 14, 2024 11:07
@Joshy-R Joshy-R force-pushed the refactor_handling_of_gate_matrices_and_inverses branch from 0c22ab7 to 3d824d7 Compare November 14, 2024 11:28
Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 96.77419% with 3 lines in your changes missing coverage. Please review.

Project coverage is 92.0%. Comparing base (868299c) to head (3d824d7).

Files with missing lines Patch % Lines
include/mqt-core/dd/Operations.hpp 94.4% 2 Missing ⚠️
include/mqt-core/ir/operations/OpType.hpp 80.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #752     +/-   ##
=======================================
- Coverage   92.1%   92.0%   -0.1%     
=======================================
  Files        125     126      +1     
  Lines      13770   13652    -118     
  Branches    2157    2164      +7     
=======================================
- Hits       12684   12565    -119     
- Misses      1086    1087      +1     
Flag Coverage Δ *Carryforward flag
cpp 91.7% <96.7%> (-0.1%) ⬇️
python 99.7% <ø> (ø) Carriedforward from 868299c

*This pull request uses carry forward flags. Click here to find out more.

Files with missing lines Coverage Δ
include/mqt-core/dd/GateMatrixDefinitions.hpp 100.0% <100.0%> (ø)
include/mqt-core/ir/operations/Operation.hpp 79.7% <100.0%> (-0.3%) ⬇️
src/circuit_optimizer/CircuitOptimizer.cpp 90.2% <100.0%> (-0.1%) ⬇️
src/dd/GateMatrixDefinitions.cpp 100.0% <100.0%> (ø)
src/dd/NoiseFunctionality.cpp 97.0% <100.0%> (-0.1%) ⬇️
src/dd/Simulation.cpp 96.5% <100.0%> (+<0.1%) ⬆️
include/mqt-core/ir/operations/OpType.hpp 85.1% <80.0%> (-6.4%) ⬇️
include/mqt-core/dd/Operations.hpp 82.6% <94.4%> (-6.0%) ⬇️

... and 1 file with indirect coverage changes

oss << "DD for gate " << op->getName() << " not available!";
throw qc::QFRException(oss.str());
}
auto [type, theta, beta, _] =

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable _ is not used.
@burgholzer burgholzer added refactor Anything related to code refactoring Core Anything related to the Core library and IR c++ Anything related to C++ code labels Nov 14, 2024
Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey 👋

Many thanks for starting to work on this! Great to see someone taking this on!
I really like the solution with the flags for indicating the inverse type of a gate and indicating whether a gate is diagonal! Very elegant.

I added some inline comments that are mostlly minor except one bigger one that might trigger some further refactorings. However, I believe these might be worth it.

One thing on top of all of that: It would be great if you could address the clang-tidy complaints.
Typically these are posted as PR comments, but that feature does not work for PRs from forks. You can still see the warnings either in the check summary here: https://github.com/cda-tum/mqt-core/pull/752/checks or when looking through the "Files Changed" tab on GitHub.

Thanks again and let me know if anything is unclear!

Comment on lines +12 to +15
enum OpTypeFlags {
OP_TYPE_INV = 1,
OP_TYPE_DIAG = 2,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum class OpTypeFlag: uint8_t {
  NONE = 0b00,
  INV  = 0b01,
  DIAG = 0b10,
};

How about the above suggestion? first of all creates a dedicated NONE entry that replaces the use of 0s throughout the code. Uses binary integers to more clearly illustrate the flag character. And makes this an enum class.

However, given that the arithmetic with different enums does seem to upset clang-tidy, it might be worth to simply define these as

constexpr unit8_t OP_TYPE_FLAG_NONE = 0b00;
constexpr unit8_t OP_TYPE_FLAG_INV  = 0b01;
constexpr unit8_t OP_TYPE_FLAG_DIAG = 0b10;

OP_TYPE_DIAG = 2,
};

constexpr static unsigned N_OP_TYPE_FLAGS_BITS = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constexpr static unsigned N_OP_TYPE_FLAGS_BITS = 2;
constexpr static unsigned NUM_OP_TYPE_FLAG_BITS = 2;

Slight naming preference for consistency.

Comment on lines +1 to +2
#define OP_TYPE_INV 0b1
#define OP_TYPE_DIAG 0b10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these really need to be defined here? Aren't these defined as enums anyway?

case qc::T: case qc::Tdg:
case qc::V: case qc::Vdg:
case qc::SX: case qc::SXdg:
case qc::I: case qc::H: case qc::X: case qc::Y: case qc::Z:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not yet completely correct and clang-tidy rightfully complains here.
I, H, X, Y, Z are all self inverse. So these need not be XORed.

I am also not a big fan of fall-through switch statements, so I'd rather have an explicit return in each of the blocks.


const auto type = op->getType();
inline std::tuple<qc::OpType, fp, fp, fp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not yet fully happy with this fixes kind of return type. In principle, gates could have an arbitrary number of parameters. The fact that currently supported gates onlly use up to three parameters is purely artificial and there are examples of gates with 4+ parameters that we might want to support in the future. Do you see any kind of way of supporting that use case here? Either via variadic lists or vectors of parameters or something completely different..

Maybe it would even be better to take this one step further and represent an operation by its number of qubits, its type, and its number of parameters. Then one could probably transform the code here to a very uniform style and consolidate it quite a bit.

Adding even one step further, this might be worth pulling from the DD submodule to the ir/operations module as a whole given the general usefulness of getting the (inverse) matrix for a given StandardOperation.

Copy link
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, clicked the wrong button 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Anything related to C++ code Core Anything related to the Core library and IR refactor Anything related to code refactoring
Projects
Status: In Progress
Status: In Progress
Development

Successfully merging this pull request may close these issues.

♻️ Refactor handling of gate matrices and inverses
2 participants