-
Notifications
You must be signed in to change notification settings - Fork 56
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
Banded unitaries #208
Merged
Merged
Banded unitaries #208
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0deaca4
First working version with tests
nquesada c5a2f0d
Adds extra test
nquesada 454dffa
Fixes linting
nquesada d233b50
Merge branch 'master' into banded_unitaries
nquesada 8d3faf6
removes unnecesary if
nquesada e07b8dc
Merge branch 'master' into banded_unitaries
nquesada 4193890
Merge branch 'master' into banded_unitaries
nquesada 5ef7a6d
Merge branch 'banded_unitaries' of github.com:XanaduAI/thewalrus into…
nquesada fe0dc17
Updates CHANGELOG
nquesada fef3932
Correct typo
nquesada aa4cf7d
Adds name
nquesada 4eb5746
Update thewalrus/tests/test_random.py
nquesada 87f4bcf
Update thewalrus/tests/test_random.py
nquesada 2a425b2
corrects typo
nquesada 94f1abd
Merge branch 'master' into banded_unitaries
nquesada 62ae1bb
Apply suggestions from code review
nquesada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2019 Xanadu Quantum Technologies Inc. | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Random tests""" | ||
import pytest | ||
|
||
import numpy as np | ||
from thewalrus.random import random_block_interferometer, random_banded_interferometer | ||
|
||
|
||
def bandwidth(A): | ||
"""Calculates the upper bandwidth of the matrix A | ||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
A (array): input matrix | ||
|
||
Returns: | ||
(int): bandwidth of matrix | ||
""" | ||
n, _ = A.shape | ||
for i in range(n): | ||
vali = np.diag(A, i) | ||
if np.allclose(vali, 0): | ||
return i - 1 | ||
return n - 1 | ||
|
||
|
||
@pytest.mark.parametrize("n", [5, 7, 8, 9]) | ||
@pytest.mark.parametrize("top_one", [True, False]) | ||
@pytest.mark.parametrize("real", [True, False]) | ||
def test_random_block(n, top_one, real): | ||
"""Test that random_block_interferometer produces a unitary with the right structure""" | ||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
U = random_block_interferometer(n, top_one=top_one, real=real) | ||
assert np.allclose(U @ U.T.conj(), np.identity(n)) | ||
if top_one: | ||
assert np.allclose(U[0, 1], 0) | ||
assert np.allclose(U[1, 0], 0) | ||
|
||
|
||
@pytest.mark.parametrize("n", [5, 7, 8, 9]) | ||
@pytest.mark.parametrize("top_one_init", [True, False]) | ||
@pytest.mark.parametrize("real", [True, False]) | ||
def test_random_banded(n, top_one_init, real): | ||
"""Test that random_banded_interferometer produces a unitary with the right structure""" | ||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for w in range(n): | ||
U = random_banded_interferometer(n, w, top_one_init=top_one_init, real=real) | ||
assert np.allclose(U @ U.T.conj(), np.identity(n)) | ||
assert w == bandwidth(U) | ||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_wrong_bandwidth(): | ||
"""Test that the correct error is raised if w > n-1""" | ||
nquesada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n = 10 | ||
w = 10 | ||
with pytest.raises(ValueError, match="The bandwidth can be at most one minus the size of the matrix."): | ||
random_banded_interferometer(n, w) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth mentioning
random_block_interferometer
also as a new feature, or is that not meant to be user-facing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too sure, seems a bit useless on its own.