Skip to content

Refactoring of synthesis of division and modulo operations #305

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

Merged

Conversation

TooMuchDakka
Copy link
Collaborator

@TooMuchDakka TooMuchDakka commented Jul 18, 2025

Description

The current implementation of the synthesis of the division and modulo operations did not create the correct gate sequence and lead to a missmatch between the expected and actual output values when simulating the synthesized circuits. With this PR the implementation of both operations is replaced with the restoring division algorithm defined in Quantum Circuit Designs of Integer Division Optimizing T-count and T-depth. This caused the following changes:

  • Both operands now need 2*N ancillary qubits to store the values of the quotient and remainder for each operation.
  • The internal SyrecSynthesis interface for the division and modulo operations had to be updated since both
    the quotient and remainder qubits need to be provided for both operations.
  • The transistor cost of both operations increased.

Note that the referenced paper also defines a non-restoring division algorithm that requires less transistors to synthesize. At the moment we are not sure whether any of the two division algorithms could be refactored to only require N ancillary qubits (to store the quotient in the division/remainder in the modulo operation) to synthesis.

Fixes #302

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.

…ence algorithm. Added basic simulation tests to verify correct synthesis of refactored operations.
@TooMuchDakka TooMuchDakka added c++ Anything related to C++ code fix Changes fixing something that was not working labels Jul 18, 2025
@@ -147,6 +147,16 @@
this->performTestExecutionForCircuitLoadedFromJson(RELATIVE_PATH_TO_TEST_CASE_DATA_JSON_FILE, this->getNameOfCurrentlyExecutedTest());
}

TYPED_TEST_P(BaseSimulationTestFixture, BinaryOperationDivision) {

Check notice

Code scanning / CodeQL

Unused static variable Note test

Static variable gtest_BinaryOperationDivision_defined_ is never read.
this->performTestExecutionForCircuitLoadedFromJson(RELATIVE_PATH_TO_TEST_CASE_DATA_JSON_FILE, this->getNameOfCurrentlyExecutedTest());
}

TYPED_TEST_P(BaseSimulationTestFixture, BinaryOperationModulo) {

Check notice

Code scanning / CodeQL

Unused static variable Note test

Static variable gtest_BinaryOperationModulo_defined_ is never read.
@TooMuchDakka TooMuchDakka marked this pull request as ready for review July 19, 2025 07:48
@TooMuchDakka TooMuchDakka requested a review from burgholzer July 19, 2025 07:49
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.

LGTM

@burgholzer burgholzer requested a review from Copilot July 19, 2025 09:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the synthesis of division and modulo operations by replacing the existing implementation with the restoring division algorithm from "Quantum Circuit Designs of Integer Division Optimizing T-count and T-depth". The changes address a mismatch between expected and actual output values in synthesized circuits.

  • Updates division and modulo synthesis to use the restoring division algorithm with proper quotient and remainder calculations
  • Modifies the SyrecSynthesis interface to require both quotient and remainder qubits for both operations
  • Adds comprehensive test coverage for division and modulo operations with updated cost expectations

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/algorithms/synthesis/syrec_synthesis.cpp Implements new restoring division algorithm for both division and modulo operations
include/algorithms/synthesis/syrec_synthesis.hpp Updates function signatures to include quotient and remainder parameters
test/unittests/simulation/test_synthesis_basic_operations.cpp Adds new test cases for division and modulo operations
test/unittests/simulation/data/test_synthesis_of_basic_operations.json Provides test data with expected input/output pairs for division and modulo
test/configs/circuits_line_aware_synthesis.json Updates expected circuit metrics for affected test circuits
test/configs/circuits_cost_aware_synthesis.json Updates expected circuit metrics for affected test circuits

Copy link

codecov bot commented Jul 19, 2025

Codecov Report

Attention: Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/algorithms/synthesis/syrec_synthesis.cpp 92.8% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@TooMuchDakka TooMuchDakka merged commit 2ea3679 into munich-quantum-toolkit:main Jul 19, 2025
17 checks passed
@TooMuchDakka TooMuchDakka deleted the div-and-mod-fix branch July 19, 2025 17:46
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 fix Changes fixing something that was not working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Synthesis of division and modulo operation generates wrong gate sequence
2 participants