Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Add ApplyUnitary operation #391

Merged
merged 26 commits into from Jan 14, 2021
Merged

Add ApplyUnitary operation #391

merged 26 commits into from Jan 14, 2021

Conversation

fedimser
Copy link
Contributor

@fedimser fedimser commented Dec 21, 2020

This PR adds Q# operation which applies arbitrary gate specified by 2^n x 2^n unitary matrix on a qubit register, as requested in #291.

It's based on an algorithm which I have implemented in Python in this repository.

Implementation consists of 3 parts:

  1. Implementing 1-qubit gate specified by 2x2 matrix - in native Q#.
  2. Matrix decomposition algorithm, which decomposes unitary matrix as product of two-level unitary matrices with indices differing exactly in 1 bit - in C#.
  3. Q# operation which uses decomposition in (2) and implements arbitrary n-qubit unitary via multiple calls to controlled (1) operation and X gates.

Complexity of this algorithm is O(N^3) and size of output is O(N^2), where N is size of matrix (N=2^n, where n is number of qubits).

@ghost
Copy link

ghost commented Dec 21, 2020

CLA assistant check
All CLA requirements met.

Copy link
Member

@msoeken msoeken left a comment

Choose a reason for hiding this comment

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

Hi @fedimser, you're initial commits look interesting! I left some initial comments.

Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/tests/Synthesis/UnitaryDecompositionTest.qs Outdated Show resolved Hide resolved
Standard/tests/Synthesis/UnitaryDecompositionTest.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
@fedimser fedimser marked this pull request as draft December 22, 2020 11:09
@fedimser
Copy link
Contributor Author

Thanks for the review.
This PR is work in progress, so I converted it to draft.

@fedimser
Copy link
Contributor Author

@msoeken, can you please advice which linear algebra library I can use in the C# code?
All I need is to be able to represent complex matrices and matrix multiplication. I can implement it from scratch on top of C# arrays, but I would prefer to use some C# equivalent of NumPy.

@msoeken
Copy link
Member

msoeken commented Dec 22, 2020

@fedimser I don't have much experience with NumPy alternatives in .NET, but you can see if NumSharp has the operations you need.

@fedimser fedimser marked this pull request as ready for review December 22, 2020 23:37
Copy link
Member

@msoeken msoeken left a comment

Choose a reason for hiding this comment

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

Thanks a lot @fedimser, this is great work! I left some comments, mostly formatting and some suggestions.

Standard/src/Synthesis/MatrixUtils.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/MatrixUtils.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/MatrixUtils.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/MatrixUtils.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/TwoLevelUnitary.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Copy link
Contributor

@cgranade cgranade left a comment

Choose a reason for hiding this comment

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

Thanks for doing this, we really appreciate your contribution! I've marked as request changes for some of the required modifications (e.g.: copyright headers), but I'm happy to help expand on or implement any of my comments.

Since this PR modifies the Q# API, I'll also go on and schedule it for the next API Review Meeting, and will comment here accordingly following that meeting. Thank you!

Standard/src/Synthesis/MatrixUtils.cs Show resolved Hide resolved
Standard/src/Synthesis/TwoLevelUnitary.cs Show resolved Hide resolved
Standard/src/Synthesis/TwoLevelUnitary.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/TwoLevelUnitary.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/TwoLevelUnitary.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/tests/Synthesis/UnitaryDecompositionTest.qs Outdated Show resolved Hide resolved
@cgranade cgranade added Status-NeedsApiReview This PR requires an API review before merging in. Pkg-Standard Issue relates to the Microsoft.Quantum.Standard package. labels Jan 4, 2021
@cgranade cgranade added this to Ready to be reviewed in API Review Scheduling Jan 4, 2021
fedimser and others added 6 commits January 5, 2021 20:03
Co-authored-by: Chris Granade <cgranade@gmail.com>
Co-authored-by: Mathias Soeken <mathias.soeken@gmail.com>
Co-authored-by: Mathias Soeken <mathias.soeken@gmail.com>
Co-authored-by: Chris Granade <cgranade@gmail.com>
Co-authored-by: Mathias Soeken <mathias.soeken@gmail.com>
@fedimser fedimser requested a review from msoeken January 5, 2021 22:10
Copy link
Member

@msoeken msoeken left a comment

Choose a reason for hiding this comment

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

Looks great to me, thanks! I left some few comments.

Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
@fedimser
Copy link
Contributor Author

fedimser commented Jan 6, 2021

Hi Mathias,

I addressed your comments.
Also I have found a bug in my algorithm, which I fixed in my Python repository, as well as here.
I added one more test for 8x8 matrix which is equivalent to 4 controlled gates (this test helped me find a bug).

One more thing to double check is that my algorithm indeed applies matrix to little-endian register. The easiest way to check that is to see what is matrix for CNOT gate, and I have a test for that.
So, if you give this matrix to ApplyUnitary:
[[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 1, 0, 0]],
then it will apply operation equivalent to CNOT(q[0], q[1]).
We can see that it swaps states 1 and 3, i.e. states with the lowest bit equal to 1. Which means qubit corresponding to thelowest bit is control qubit. And we see that control qubit is q[0]. So, the qubit with index 0 encodes the lowest bit, as it should.

@fedimser fedimser requested a review from msoeken January 6, 2021 23:29
Copy link
Member

@msoeken msoeken left a comment

Choose a reason for hiding this comment

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

Just some formatting suggestions.

Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/tests/Synthesis/UnitaryDecompositionTest.qs Outdated Show resolved Hide resolved
@fedimser fedimser requested a review from msoeken January 7, 2021 09:34
@cgranade cgranade moved this from Ready to be reviewed to Reviewed: approved in API Review Scheduling Jan 8, 2021
Copy link
Contributor

@cgranade cgranade left a comment

Choose a reason for hiding this comment

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

This looks really good, thanks for the contribution! I've left a few last comments as suggestions that would be good to bring in before merging this to main, but otherwise I think this is getting pretty close now that the API review has completed.

Thank you again!

Standard/src/Synthesis/UnitaryDecomposition.qs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
Standard/src/Synthesis/UnitaryDecomposition.cs Outdated Show resolved Hide resolved
@fedimser
Copy link
Contributor Author

@cgranade, I addressed your comments. I think this PR is now ready to be merged.

Copy link
Contributor

@cgranade cgranade left a comment

Choose a reason for hiding this comment

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

Looks good, thank you for the contribution! ♥

@cgranade
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@cgranade
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@msoeken msoeken merged commit 958bca6 into microsoft:main Jan 14, 2021
@cgranade cgranade moved this from Reviewed: approved to Completed in API Review Scheduling Feb 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Pkg-Standard Issue relates to the Microsoft.Quantum.Standard package. Status-NeedsApiReview This PR requires an API review before merging in.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants