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

Checks if DiagonalGate has modulus 1 #8098

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit/extensions/quantum_initializer/diagonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, diag):
raise QiskitError(
"Not all of the diagonal entries can be converted to complex numbers."
) from ex
if not np.abs(z) - 1 < _EPS:
if not np.abs(np.abs(z) - 1) < _EPS:
raise QiskitError("A diagonal entry has not absolute value one.")
# Create new gate.
super().__init__("diagonal", int(num_action_qubits), diag)
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/3842-14af3f8d922a7253.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Extra validation was added to :class:`.DiagonalGate` to check the argument has modulus one.
9 changes: 9 additions & 0 deletions test/python/circuit/test_diagonal_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

from qiskit import QuantumCircuit, QuantumRegister, BasicAer, execute

from qiskit import QiskitError
from qiskit.test import QiskitTestCase
from qiskit.compiler import transpile
from qiskit.extensions.quantum_initializer import DiagonalGate
from qiskit.quantum_info.operators.predicates import matrix_equal


Expand Down Expand Up @@ -54,6 +56,13 @@ def test_diag_gate(self):
unitary_desired = _get_diag_gate_matrix(diag)
self.assertTrue(matrix_equal(unitary, unitary_desired, ignore_phase=False))

def test_mod1_entries(self):
"""Test that diagonal raises if entries do not have modules of 1."""
from qiskit.quantum_info.operators.predicates import ATOL_DEFAULT, RTOL_DEFAULT

with self.assertRaises(QiskitError):
DiagonalGate([1, 1 - 2 * ATOL_DEFAULT - RTOL_DEFAULT])


def _get_diag_gate_matrix(diag):
return np.diagflat(diag)
Expand Down