Skip to content

Commit

Permalink
Raise an error if the section size is invalid
Browse files Browse the repository at this point in the history
Co-authored-by: Abdalla01001 <[email protected]>
Co-authored-by: Tarun-Kumar07 <[email protected]>
  • Loading branch information
3 people committed Jul 3, 2024
1 parent b8f50f2 commit 42a0e27
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 6 additions & 5 deletions qiskit/synthesis/linear/cnot_synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import numpy as np
from qiskit.circuit import QuantumCircuit
from qiskit.exceptions import QiskitError

from qiskit._accelerate.synthesis.linear import synth_cnot_count_full_pmh as fast_pmh

Expand Down Expand Up @@ -48,17 +47,19 @@ def synth_cnot_count_full_pmh(
A CX-only circuit implementing the linear transformation.
Raises:
QiskitError: when variable ``state`` isn't of type ``numpy.ndarray``.
ValueError: When ``section_size`` is larger than the number of columns.
References:
1. Patel, Ketan N., Igor L. Markov, and John P. Hayes,
*Optimal synthesis of linear reversible circuits*,
Quantum Information & Computation 8.3 (2008): 282-294.
`arXiv:quant-ph/0302002 [quant-ph] <https://arxiv.org/abs/quant-ph/0302002>`_
"""
if not isinstance(state, (list, np.ndarray)):
raise QiskitError(
f"state should be of type list or numpy.ndarray, but was of the type {type(state)}"
normalized = np.asarray(state).astype(bool)
if section_size is not None and normalized.shape[1] < section_size:
raise ValueError(
f"The section_size ({section_size}) is cannot be larger than the number of columns "
f"({normalized.shape[1]})."
)

# call Rust implementation with normalized input
Expand Down
24 changes: 24 additions & 0 deletions test/python/synthesis/test_linear_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
binary_matmul,
)
from qiskit.synthesis.linear.linear_circuits_utils import transpose_cx_circ, optimize_cx_4_options
from qiskit.quantum_info import Operator
from test import QiskitTestCase # pylint: disable=wrong-import-order


Expand Down Expand Up @@ -139,6 +140,29 @@ def test_synth_lnn_kms(self, num_qubits):
dist = abs(q0 - q1)
self.assertEqual(dist, 1)

def test_pmh_section_sizes(self):
"""Test the PMH algorithm for different section sizes.
Regression test of Qiskit/qiskit#12166.
"""
n = 5
qc = QuantumCircuit(n)
for i in range(n - 1):
qc.cx(i, i + 1)
expected = Operator(qc)

mat = LinearFunction(qc).linear
for section_size in range(1, n + 1):
synthesized = synth_cnot_count_full_pmh(mat, section_size=section_size)
with self.subTest(section_size=section_size):
self.assertEqual(expected, Operator(synthesized))

def test_pmh_invalid_section_size(self):
"""Test that a section size larger than the number of qubits raises an error."""
mat = [[True, False], [True, False]]
with self.assertRaises(ValueError):
_ = synth_cnot_count_full_pmh(mat, section_size=3)


if __name__ == "__main__":
unittest.main()

0 comments on commit 42a0e27

Please sign in to comment.