Skip to content

Commit

Permalink
Skip classical function tests if tweedledum isn't present (#8876) (#8881
Browse files Browse the repository at this point in the history
)

* Skip classical function tests if tweedledum isn't present

This commit fixes an oversight in #8849 and #8818 which made tweedledum
optional for arm64 macOS systems which was the unittest suite still
unconditionally depends on tweedledum being installed to run the
classical function compiler tests. This commit fixes this by making the
tests skip if tweedledum isn't installed so that M1 mac users are able
to run the full test suite again.

* Skip other tests requiring tweedledum

(cherry picked from commit 75fe9bb)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
mergify[bot] and mtreinish authored Oct 12, 2022
1 parent 6d56cc5 commit d137756
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 54 deletions.
3 changes: 3 additions & 0 deletions test/python/algorithms/test_grover.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from qiskit.circuit.library import GroverOperator, PhaseOracle
from qiskit.primitives import Sampler
from qiskit.quantum_info import Operator, Statevector
from qiskit.utils.optionals import HAS_TWEEDLEDUM


@ddt
Expand Down Expand Up @@ -100,6 +101,7 @@ def setUp(self):
self._sampler = Sampler()
self._sampler_with_shots = Sampler(options={"shots": 1024, "seed": 123})

@unittest.skipUnless(HAS_TWEEDLEDUM, "tweedledum required for this test")
@data("ideal", "shots", False)
def test_implicit_phase_oracle_is_good_state(self, use_sampler):
"""Test implicit default for is_good_state with PhaseOracle."""
Expand Down Expand Up @@ -278,6 +280,7 @@ def test_max_probability(self, use_sampler):
result = grover.amplify(problem)
self.assertAlmostEqual(result.max_probability, 1.0)

@unittest.skipUnless(HAS_TWEEDLEDUM, "tweedledum required for this test")
@data("ideal", "shots", False)
def test_oracle_evaluation(self, use_sampler):
"""Test oracle_evaluation for PhaseOracle"""
Expand Down
2 changes: 2 additions & 0 deletions test/python/circuit/library/test_phase_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
from qiskit.test.base import QiskitTestCase
from qiskit.circuit.library import PhaseOracle
from qiskit.quantum_info import Statevector
from qiskit.utils.optionals import HAS_TWEEDLEDUM


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests")
@ddt
class TestPhaseOracle(QiskitTestCase):
"""Test phase oracle object."""
Expand Down
3 changes: 3 additions & 0 deletions test/python/circuit/test_extensions_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from qiskit import BasicAer
from qiskit.quantum_info import Pauli
from qiskit.quantum_info.operators.predicates import matrix_equal, is_unitary_matrix
from qiskit.utils.optionals import HAS_TWEEDLEDUM


class TestStandard1Q(QiskitTestCase):
Expand Down Expand Up @@ -1399,6 +1400,7 @@ def test_cswap_reg_reg_inv(self):
class TestStandardMethods(QiskitTestCase):
"""Standard Extension Test."""

@unittest.skipUnless(HAS_TWEEDLEDUM, "tweedledum required for this test")
def test_to_matrix(self):
"""test gates implementing to_matrix generate matrix which matches definition."""
from qiskit.circuit.library.pauli_evolution import PauliEvolutionGate
Expand Down Expand Up @@ -1444,6 +1446,7 @@ def test_to_matrix(self):
self.assertTrue(matrix_equal(definition_unitary, gate_matrix, ignore_phase=True))
self.assertTrue(is_unitary_matrix(gate_matrix))

@unittest.skipUnless(HAS_TWEEDLEDUM, "tweedledum required for this test")
def test_to_matrix_op(self):
"""test gates implementing to_matrix generate matrix which matches
definition using Operator."""
Expand Down
59 changes: 26 additions & 33 deletions test/python/classical_function_compiler/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,40 @@

"""These examples should be handle by the classicalfunction compiler"""

from qiskit.circuit.classicalfunction.types import Int1
from qiskit.utils.optionals import HAS_TWEEDLEDUM

if HAS_TWEEDLEDUM:
from qiskit.circuit.classicalfunction.types import Int1

def identity(a: Int1) -> Int1:
return a
def identity(a: Int1) -> Int1:
return a

def bit_and(a: Int1, b: Int1) -> Int1:
return a & b

def bit_and(a: Int1, b: Int1) -> Int1:
return a & b
def bit_or(a: Int1, b: Int1) -> Int1:
return a | b

def bool_or(a: Int1, b: Int1) -> Int1:
return a or b

def bit_or(a: Int1, b: Int1) -> Int1:
return a | b
def bool_not(a: Int1) -> Int1:
return not a

def and_and(a: Int1, b: Int1, c: Int1) -> Int1:
return a and b and c

def bool_or(a: Int1, b: Int1) -> Int1:
return a or b
def multiple_binop(a: Int1, b: Int1) -> Int1:
return (a or b) | (b & a) and (a & b)

def id_assing(a: Int1) -> Int1:
b = a
return b

def bool_not(a: Int1) -> Int1:
return not a
def example1(a: Int1, b: Int1) -> Int1:
c = a & b
d = b | a
return c ^ a | d


def and_and(a: Int1, b: Int1, c: Int1) -> Int1:
return a and b and c


def multiple_binop(a: Int1, b: Int1) -> Int1:
return (a or b) | (b & a) and (a & b)


def id_assing(a: Int1) -> Int1:
b = a
return b


def example1(a: Int1, b: Int1) -> Int1:
c = a & b
d = b | a
return c ^ a | d


def grover_oracle(a: Int1, b: Int1, c: Int1, d: Int1) -> Int1:
return not a and b and not c and d
def grover_oracle(a: Int1, b: Int1, c: Int1, d: Int1) -> Int1:
return not a and b and not c and d
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

from qiskit.test.base import QiskitTestCase
from qiskit import execute, BasicAer
from qiskit.circuit.classicalfunction.boolean_expression import BooleanExpression
from qiskit.utils.optionals import HAS_TWEEDLEDUM

if HAS_TWEEDLEDUM:
from qiskit.circuit.classicalfunction.boolean_expression import BooleanExpression


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
@ddt
class TestBooleanExpression(QiskitTestCase):
"""Test boolean expression."""
Expand Down Expand Up @@ -68,6 +72,7 @@ def test_synth(self, expression, expected):
self.assertEqual(bool(int(result)), expected)


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestBooleanExpressionDIMACS(QiskitTestCase):
"""Loading from a cnf file"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
# that they have been altered from the originals.

"""Tests ClassicalFunction as a gate."""
from qiskit.test import QiskitTestCase
import unittest

from qiskit.circuit.classicalfunction import classical_function as compile_classical_function
from qiskit.test import QiskitTestCase

from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import XGate
from qiskit.utils.optionals import HAS_TWEEDLEDUM

from . import examples
if HAS_TWEEDLEDUM:
from . import examples
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestOracleDecomposition(QiskitTestCase):
"""Tests ClassicalFunction.decomposition."""

Expand Down
11 changes: 8 additions & 3 deletions test/python/classical_function_compiler/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
# that they have been altered from the originals.

"""Tests the classicalfunction parser."""
from qiskit.circuit.classicalfunction import ClassicalFunctionParseError
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function
import unittest

from qiskit.test import QiskitTestCase
from . import bad_examples as examples
from qiskit.utils.optionals import HAS_TWEEDLEDUM

if HAS_TWEEDLEDUM:
from . import bad_examples as examples
from qiskit.circuit.classicalfunction import ClassicalFunctionParseError
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestParseFail(QiskitTestCase):
"""Tests bad_examples with the classicalfunction parser."""

Expand Down
14 changes: 10 additions & 4 deletions test/python/classical_function_compiler/test_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@
# that they have been altered from the originals.

"""Tests LogicNetwork.simulate method."""
import unittest
from ddt import ddt, data
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function
from qiskit.test import QiskitTestCase
from .utils import get_truthtable_from_function, example_list
from qiskit.utils.optionals import HAS_TWEEDLEDUM

from . import utils

if HAS_TWEEDLEDUM:
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
@ddt
class TestSimulate(QiskitTestCase):
"""Tests LogicNetwork.simulate method"""

@data(*example_list())
@data(*utils.example_list())
def test_(self, a_callable):
"""Tests LogicSimulate.simulate() on all the examples"""
network = compile_classical_function(a_callable)
truth_table = network.simulate_all()
self.assertEqual(truth_table, get_truthtable_from_function(a_callable))
self.assertEqual(truth_table, utils.get_truthtable_from_function(a_callable))
8 changes: 6 additions & 2 deletions test/python/classical_function_compiler/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@
# that they have been altered from the originals.

"""Tests classicalfunction compiler synthesis."""
import unittest
from qiskit.test import QiskitTestCase

from qiskit.circuit.classicalfunction import classical_function as compile_classical_function

from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import XGate
from qiskit.utils.optionals import HAS_TWEEDLEDUM

from . import examples
if HAS_TWEEDLEDUM:
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function
from . import examples


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestSynthesis(QiskitTestCase):
"""Tests ClassicalFunction.synth method."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
# that they have been altered from the originals.

"""Tests LogicNetwork.Tweedledum2Qiskit converter."""
from tweedledum.ir import Circuit
from tweedledum.operators import X
import unittest

from qiskit.utils.optionals import HAS_TWEEDLEDUM
from qiskit.test import QiskitTestCase

from qiskit.circuit.classicalfunction.utils import tweedledum2qiskit
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library.standard_gates import XGate

if HAS_TWEEDLEDUM:
from qiskit.circuit.classicalfunction.utils import tweedledum2qiskit

from tweedledum.ir import Circuit
from tweedledum.operators import X


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTweedledum2Qiskit(QiskitTestCase):
"""Tests qiskit.transpiler.classicalfunction.utils.tweedledum2qiskit function."""

Expand Down
12 changes: 9 additions & 3 deletions test/python/classical_function_compiler/test_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
# that they have been altered from the originals.

"""Tests classicalfunction compiler type checker."""
import unittest

from qiskit.test import QiskitTestCase
from qiskit.circuit.classicalfunction import ClassicalFunctionCompilerTypeError
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function
from qiskit.utils.optionals import HAS_TWEEDLEDUM

from . import examples, bad_examples
if HAS_TWEEDLEDUM:
from . import examples, bad_examples
from qiskit.circuit.classicalfunction import ClassicalFunctionCompilerTypeError
from qiskit.circuit.classicalfunction import classical_function as compile_classical_function


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTypeCheck(QiskitTestCase):
"""Tests classicalfunction compiler type checker (good examples)."""

Expand Down Expand Up @@ -66,6 +71,7 @@ def test_bool_or(self):
)


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestTypeCheckFail(QiskitTestCase):
"""Tests classicalfunction compiler type checker (bad examples)."""

Expand Down
7 changes: 5 additions & 2 deletions test/python/classical_function_compiler/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Tests .utils.get_truthtable_from_function function"""
import unittest

from qiskit.test import QiskitTestCase
from qiskit.utils.optionals import HAS_TWEEDLEDUM
from .utils import get_truthtable_from_function
from .examples import grover_oracle
from . import examples


@unittest.skipUnless(HAS_TWEEDLEDUM, "Tweedledum is required for these tests.")
class TestGetTruthtableFromFunction(QiskitTestCase):
"""Tests .utils.get_truthtable_from_function function"""

def test_grover_oracle(self):
"""Tests get_truthtable_from_function with examples.grover_oracle"""
truth_table = get_truthtable_from_function(grover_oracle)
truth_table = get_truthtable_from_function(examples.grover_oracle)
self.assertEqual(truth_table, "0000010000000000")

0 comments on commit d137756

Please sign in to comment.