Skip to content

Commit

Permalink
Checks if DiagonalGate has modulus 1 (Qiskit#8098)
Browse files Browse the repository at this point in the history
* Checks if DiagonalGate has modulus 1 - Fixes Qiskit#3842

Co-authored-by: ewinston <ewinston@users.noreply.github.com>
Co-authored-by: hhyap <hhyap@users.noreply.github.com>

* reno

* Fix release-note typo

Co-authored-by: ewinston <ewinston@users.noreply.github.com>
Co-authored-by: hhyap <hhyap@users.noreply.github.com>
Co-authored-by: Jake Lishman <jake@binhbar.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored May 23, 2022
1 parent 8360c9b commit d2e3340
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
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

0 comments on commit d2e3340

Please sign in to comment.