Skip to content

Commit

Permalink
Avoid UserWarning during unittests (#332)
Browse files Browse the repository at this point in the history
The FermionicOp is emitting a UserWarning because we plan to change the
default `display_format` from 'dense' to 'sparse'. This warning will
only be emitted if the user did not actively specify the
`display_format` themselves. Hence, explicitly setting it during tests
will avoid the warnings.
  • Loading branch information
mrossinek authored Aug 18, 2021
1 parent ad84f3e commit 8ebde5c
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 168 deletions.
2 changes: 1 addition & 1 deletion qiskit_nature/circuit/library/ansatzes/ucc.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _build_fermionic_excitation_ops(self, excitations: Sequence) -> List[Fermion
label[occ] = "+"
for unocc in exc[1]:
label[unocc] = "-"
op = FermionicOp("".join(label))
op = FermionicOp("".join(label), display_format="dense")
op -= op.adjoint()
# we need to account for an additional imaginary phase in the exponent (see also
# `PauliTrotterEvolution.convert`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(

# encode the bitstring as a `FermionicOp`
label = ["+" if bit else "I" for bit in bitstr]
bitstr_op = FermionicOp("".join(label))
bitstr_op = FermionicOp("".join(label), display_format="dense")

# map the `FermionicOp` to a qubit operator
qubit_op: PauliSumOp = qubit_converter.convert_match(bitstr_op)
Expand Down
1 change: 0 additions & 1 deletion test/algorithms/pes_samplers/test_bopes_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ def test_vqe_bootstrap(self):
-1.137302817836066,
-1.1341458916990401,
]
print(result.energies)
np.testing.assert_almost_equal(result.points, ref_points)
np.testing.assert_almost_equal(result.energies, ref_energies)

Expand Down
24 changes: 12 additions & 12 deletions test/circuit/library/ansatzes/test_puccd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class TestPUCC(QiskitNatureTestCase):

@unpack
@data(
(4, (1, 1), [FermionicOp([("+-+-", 1j), ("-+-+", -1j)])]),
(4, (1, 1), [FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense")]),
(
8,
(2, 2),
[
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)]),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)]),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)]),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)]),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)], display_format="dense"),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)], display_format="dense"),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)], display_format="dense"),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)], display_format="dense"),
],
),
)
Expand All @@ -61,27 +61,27 @@ def test_puccd_ansatz(self, num_spin_orbitals, num_particles, expect):
(1, 1),
(True, True),
[
FermionicOp([("+-II", 1j), ("-+II", 1j)]),
FermionicOp([("II+-", 1j), ("II-+", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("+-II", 1j), ("-+II", 1j)], display_format="dense"),
FermionicOp([("II+-", 1j), ("II-+", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
(
4,
(1, 1),
(True, False),
[
FermionicOp([("+-II", 1j), ("-+II", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("+-II", 1j), ("-+II", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
(
4,
(1, 1),
(False, True),
[
FermionicOp([("II+-", 1j), ("II-+", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("II+-", 1j), ("II-+", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
)
Expand Down
36 changes: 18 additions & 18 deletions test/circuit/library/ansatzes/test_succd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ class TestSUCCD(QiskitNatureTestCase):

@unpack
@data(
(4, (1, 1), [FermionicOp([("+-+-", 1j), ("-+-+", -1j)])]),
(4, (1, 1), [FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense")]),
(
8,
(2, 2),
[
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)]),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)]),
FermionicOp([("+I-II+-I", 1j), ("-I+II-+I", -1j)]),
FermionicOp([("+I-II+I-", 1j), ("-I+II-I+", -1j)]),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)]),
FermionicOp([("+II-I+-I", 1j), ("-II+I-+I", -1j)]),
FermionicOp([("+II-I+I-", 1j), ("-II+I-I+", -1j)]),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)]),
FermionicOp([("I+-II+I-", 1j), ("I-+II-I+", -1j)]),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)]),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)], display_format="dense"),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)], display_format="dense"),
FermionicOp([("+I-II+-I", 1j), ("-I+II-+I", -1j)], display_format="dense"),
FermionicOp([("+I-II+I-", 1j), ("-I+II-I+", -1j)], display_format="dense"),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)], display_format="dense"),
FermionicOp([("+II-I+-I", 1j), ("-II+I-+I", -1j)], display_format="dense"),
FermionicOp([("+II-I+I-", 1j), ("-II+I-I+", -1j)], display_format="dense"),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)], display_format="dense"),
FermionicOp([("I+-II+I-", 1j), ("I-+II-I+", -1j)], display_format="dense"),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)], display_format="dense"),
],
),
)
Expand All @@ -67,27 +67,27 @@ def test_succd_ansatz(self, num_spin_orbitals, num_particles, expect):
(1, 1),
(True, True),
[
FermionicOp([("+-II", 1j), ("-+II", 1j)]),
FermionicOp([("II+-", 1j), ("II-+", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("+-II", 1j), ("-+II", 1j)], display_format="dense"),
FermionicOp([("II+-", 1j), ("II-+", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
(
4,
(1, 1),
(True, False),
[
FermionicOp([("+-II", 1j), ("-+II", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("+-II", 1j), ("-+II", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
(
4,
(1, 1),
(False, True),
[
FermionicOp([("II+-", 1j), ("II-+", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("II+-", 1j), ("II-+", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
)
Expand Down
33 changes: 19 additions & 14 deletions test/circuit/library/ansatzes/test_ucc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

from test import QiskitNatureTestCase

from ddt import ddt, data, unpack
from ddt import data, ddt, unpack

from qiskit_nature.circuit.library import UCC
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper
from qiskit_nature.operators.second_quantization import FermionicOp
from qiskit_nature.converters.second_quantization import QubitConverter


def assert_ucc_like_ansatz(test_case, ansatz, num_spin_orbitals, expected_ops):
Expand Down Expand Up @@ -48,27 +48,32 @@ class TestUCC(QiskitNatureTestCase):
8,
(2, 2),
[
FermionicOp([("++--+I-I", 1j), ("--++-I+I", 1j)]),
FermionicOp([("++--+II-", 1j), ("--++-II+", 1j)]),
FermionicOp([("++--I+-I", 1j), ("--++I-+I", 1j)]),
FermionicOp([("++--I+I-", 1j), ("--++I-I+", 1j)]),
FermionicOp([("+I-I++--", 1j), ("-I+I--++", 1j)]),
FermionicOp([("+II-++--", 1j), ("-II+--++", 1j)]),
FermionicOp([("I+-I++--", 1j), ("I-+I--++", 1j)]),
FermionicOp([("I+I-++--", 1j), ("I-I+--++", 1j)]),
FermionicOp([("++--+I-I", 1j), ("--++-I+I", 1j)], display_format="dense"),
FermionicOp([("++--+II-", 1j), ("--++-II+", 1j)], display_format="dense"),
FermionicOp([("++--I+-I", 1j), ("--++I-+I", 1j)], display_format="dense"),
FermionicOp([("++--I+I-", 1j), ("--++I-I+", 1j)], display_format="dense"),
FermionicOp([("+I-I++--", 1j), ("-I+I--++", 1j)], display_format="dense"),
FermionicOp([("+II-++--", 1j), ("-II+--++", 1j)], display_format="dense"),
FermionicOp([("I+-I++--", 1j), ("I-+I--++", 1j)], display_format="dense"),
FermionicOp([("I+I-++--", 1j), ("I-I+--++", 1j)], display_format="dense"),
],
),
(
"t",
8,
(2, 1),
[
FermionicOp([("++--+-II", 1j), ("--++-+II", 1j)]),
FermionicOp([("++--+I-I", 1j), ("--++-I+I", 1j)]),
FermionicOp([("++--+II-", 1j), ("--++-II+", 1j)]),
FermionicOp([("++--+-II", 1j), ("--++-+II", 1j)], display_format="dense"),
FermionicOp([("++--+I-I", 1j), ("--++-I+I", 1j)], display_format="dense"),
FermionicOp([("++--+II-", 1j), ("--++-II+", 1j)], display_format="dense"),
],
),
("q", 8, (2, 2), [FermionicOp([("++--++--", 1j), ("--++--++", -1j)])]),
(
"q",
8,
(2, 2),
[FermionicOp([("++--++--", 1j), ("--++--++", -1j)], display_format="dense")],
),
# TODO: add more edge cases?
)
def test_ucc_ansatz(self, excitations, num_spin_orbitals, num_particles, expect):
Expand Down
98 changes: 49 additions & 49 deletions test/circuit/library/ansatzes/test_uccsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,67 +33,67 @@ class TestUCCSD(QiskitNatureTestCase):
4,
(1, 1),
[
FermionicOp([("+-II", 1j), ("-+II", 1j)]),
FermionicOp([("II+-", 1j), ("II-+", 1j)]),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)]),
FermionicOp([("+-II", 1j), ("-+II", 1j)], display_format="dense"),
FermionicOp([("II+-", 1j), ("II-+", 1j)], display_format="dense"),
FermionicOp([("+-+-", 1j), ("-+-+", -1j)], display_format="dense"),
],
),
(
8,
(2, 2),
[
FermionicOp([("+I-IIIII", 1j), ("-I+IIIII", 1j)]),
FermionicOp([("+II-IIII", 1j), ("-II+IIII", 1j)]),
FermionicOp([("I+-IIIII", 1j), ("I-+IIIII", 1j)]),
FermionicOp([("I+I-IIII", 1j), ("I-I+IIII", 1j)]),
FermionicOp([("IIII+I-I", 1j), ("IIII-I+I", 1j)]),
FermionicOp([("IIII+II-", 1j), ("IIII-II+", 1j)]),
FermionicOp([("IIIII+-I", 1j), ("IIIII-+I", 1j)]),
FermionicOp([("IIIII+I-", 1j), ("IIIII-I+", 1j)]),
FermionicOp([("++--IIII", 1j), ("--++IIII", -1j)]),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)]),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)]),
FermionicOp([("+I-II+-I", 1j), ("-I+II-+I", -1j)]),
FermionicOp([("+I-II+I-", 1j), ("-I+II-I+", -1j)]),
FermionicOp([("+II-+I-I", 1j), ("-II+-I+I", -1j)]),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)]),
FermionicOp([("+II-I+-I", 1j), ("-II+I-+I", -1j)]),
FermionicOp([("+II-I+I-", 1j), ("-II+I-I+", -1j)]),
FermionicOp([("I+-I+I-I", 1j), ("I-+I-I+I", -1j)]),
FermionicOp([("I+-I+II-", 1j), ("I-+I-II+", -1j)]),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)]),
FermionicOp([("I+-II+I-", 1j), ("I-+II-I+", -1j)]),
FermionicOp([("I+I-+I-I", 1j), ("I-I+-I+I", -1j)]),
FermionicOp([("I+I-+II-", 1j), ("I-I+-II+", -1j)]),
FermionicOp([("I+I-I+-I", 1j), ("I-I+I-+I", -1j)]),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)]),
FermionicOp([("IIII++--", 1j), ("IIII--++", -1j)]),
FermionicOp([("+I-IIIII", 1j), ("-I+IIIII", 1j)], display_format="dense"),
FermionicOp([("+II-IIII", 1j), ("-II+IIII", 1j)], display_format="dense"),
FermionicOp([("I+-IIIII", 1j), ("I-+IIIII", 1j)], display_format="dense"),
FermionicOp([("I+I-IIII", 1j), ("I-I+IIII", 1j)], display_format="dense"),
FermionicOp([("IIII+I-I", 1j), ("IIII-I+I", 1j)], display_format="dense"),
FermionicOp([("IIII+II-", 1j), ("IIII-II+", 1j)], display_format="dense"),
FermionicOp([("IIIII+-I", 1j), ("IIIII-+I", 1j)], display_format="dense"),
FermionicOp([("IIIII+I-", 1j), ("IIIII-I+", 1j)], display_format="dense"),
FermionicOp([("++--IIII", 1j), ("--++IIII", -1j)], display_format="dense"),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)], display_format="dense"),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)], display_format="dense"),
FermionicOp([("+I-II+-I", 1j), ("-I+II-+I", -1j)], display_format="dense"),
FermionicOp([("+I-II+I-", 1j), ("-I+II-I+", -1j)], display_format="dense"),
FermionicOp([("+II-+I-I", 1j), ("-II+-I+I", -1j)], display_format="dense"),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)], display_format="dense"),
FermionicOp([("+II-I+-I", 1j), ("-II+I-+I", -1j)], display_format="dense"),
FermionicOp([("+II-I+I-", 1j), ("-II+I-I+", -1j)], display_format="dense"),
FermionicOp([("I+-I+I-I", 1j), ("I-+I-I+I", -1j)], display_format="dense"),
FermionicOp([("I+-I+II-", 1j), ("I-+I-II+", -1j)], display_format="dense"),
FermionicOp([("I+-II+-I", 1j), ("I-+II-+I", -1j)], display_format="dense"),
FermionicOp([("I+-II+I-", 1j), ("I-+II-I+", -1j)], display_format="dense"),
FermionicOp([("I+I-+I-I", 1j), ("I-I+-I+I", -1j)], display_format="dense"),
FermionicOp([("I+I-+II-", 1j), ("I-I+-II+", -1j)], display_format="dense"),
FermionicOp([("I+I-I+-I", 1j), ("I-I+I-+I", -1j)], display_format="dense"),
FermionicOp([("I+I-I+I-", 1j), ("I-I+I-I+", -1j)], display_format="dense"),
FermionicOp([("IIII++--", 1j), ("IIII--++", -1j)], display_format="dense"),
],
),
(
8,
(2, 1),
[
FermionicOp([("+I-IIIII", 1j), ("-I+IIIII", 1j)]),
FermionicOp([("+II-IIII", 1j), ("-II+IIII", 1j)]),
FermionicOp([("I+-IIIII", 1j), ("I-+IIIII", 1j)]),
FermionicOp([("I+I-IIII", 1j), ("I-I+IIII", 1j)]),
FermionicOp([("IIII+-II", 1j), ("IIII-+II", 1j)]),
FermionicOp([("IIII+I-I", 1j), ("IIII-I+I", 1j)]),
FermionicOp([("IIII+II-", 1j), ("IIII-II+", 1j)]),
FermionicOp([("++--IIII", 1j), ("--++IIII", -1j)]),
FermionicOp([("+I-I+-II", 1j), ("-I+I-+II", -1j)]),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)]),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)]),
FermionicOp([("+II-+-II", 1j), ("-II+-+II", -1j)]),
FermionicOp([("+II-+I-I", 1j), ("-II+-I+I", -1j)]),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)]),
FermionicOp([("I+-I+-II", 1j), ("I-+I-+II", -1j)]),
FermionicOp([("I+-I+I-I", 1j), ("I-+I-I+I", -1j)]),
FermionicOp([("I+-I+II-", 1j), ("I-+I-II+", -1j)]),
FermionicOp([("I+I-+-II", 1j), ("I-I+-+II", -1j)]),
FermionicOp([("I+I-+I-I", 1j), ("I-I+-I+I", -1j)]),
FermionicOp([("I+I-+II-", 1j), ("I-I+-II+", -1j)]),
FermionicOp([("+I-IIIII", 1j), ("-I+IIIII", 1j)], display_format="dense"),
FermionicOp([("+II-IIII", 1j), ("-II+IIII", 1j)], display_format="dense"),
FermionicOp([("I+-IIIII", 1j), ("I-+IIIII", 1j)], display_format="dense"),
FermionicOp([("I+I-IIII", 1j), ("I-I+IIII", 1j)], display_format="dense"),
FermionicOp([("IIII+-II", 1j), ("IIII-+II", 1j)], display_format="dense"),
FermionicOp([("IIII+I-I", 1j), ("IIII-I+I", 1j)], display_format="dense"),
FermionicOp([("IIII+II-", 1j), ("IIII-II+", 1j)], display_format="dense"),
FermionicOp([("++--IIII", 1j), ("--++IIII", -1j)], display_format="dense"),
FermionicOp([("+I-I+-II", 1j), ("-I+I-+II", -1j)], display_format="dense"),
FermionicOp([("+I-I+I-I", 1j), ("-I+I-I+I", -1j)], display_format="dense"),
FermionicOp([("+I-I+II-", 1j), ("-I+I-II+", -1j)], display_format="dense"),
FermionicOp([("+II-+-II", 1j), ("-II+-+II", -1j)], display_format="dense"),
FermionicOp([("+II-+I-I", 1j), ("-II+-I+I", -1j)], display_format="dense"),
FermionicOp([("+II-+II-", 1j), ("-II+-II+", -1j)], display_format="dense"),
FermionicOp([("I+-I+-II", 1j), ("I-+I-+II", -1j)], display_format="dense"),
FermionicOp([("I+-I+I-I", 1j), ("I-+I-I+I", -1j)], display_format="dense"),
FermionicOp([("I+-I+II-", 1j), ("I-+I-II+", -1j)], display_format="dense"),
FermionicOp([("I+I-+-II", 1j), ("I-I+-+II", -1j)], display_format="dense"),
FermionicOp([("I+I-+I-I", 1j), ("I-I+-I+I", -1j)], display_format="dense"),
FermionicOp([("I+I-+II-", 1j), ("I-I+-II+", -1j)], display_format="dense"),
],
),
)
Expand Down
11 changes: 6 additions & 5 deletions test/converters/second_quantization/test_qubit_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
import contextlib
import io
import unittest
from typing import Optional, List, cast

from test import QiskitNatureTestCase
from typing import List, Optional, cast

from qiskit.opflow import X, Y, Z, I, PauliSumOp, Z2Symmetries
from qiskit.opflow import I, PauliSumOp, X, Y, Z, Z2Symmetries

from qiskit_nature import QiskitNatureError
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.drivers.second_quantization import HDF5Driver
from qiskit_nature.mappers.second_quantization import JordanWignerMapper, ParityMapper
from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.operators.second_quantization import FermionicOp
from qiskit_nature.problems.second_quantization import ElectronicStructureProblem
from qiskit_nature.properties.second_quantization.electronic import ParticleNumber
Expand Down Expand Up @@ -170,7 +169,9 @@ def test_two_qubit_reduction(self):

# Regression test against https://github.com/Qiskit/qiskit-nature/issues/271
with self.subTest("Two qubit reduction skipped when operator too small"):
small_op = FermionicOp([("N_0", 1.0), ("E_1", 1.0)], register_length=2)
small_op = FermionicOp(
[("N_0", 1.0), ("E_1", 1.0)], register_length=2, display_format="sparse"
)
expected_op = 1.0 * (I ^ I) - 0.5 * (I ^ Z) + 0.5 * (Z ^ Z)
with contextlib.redirect_stderr(io.StringIO()) as out:
qubit_op = qubit_conv.convert(small_op)
Expand Down
Loading

0 comments on commit 8ebde5c

Please sign in to comment.