From 8ebde5ca47d7f70f2ece5ead4eb48fb2f8f90269 Mon Sep 17 00:00:00 2001 From: Max Rossmannek Date: Wed, 18 Aug 2021 17:42:40 +0200 Subject: [PATCH] Avoid UserWarning during unittests (#332) 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. --- qiskit_nature/circuit/library/ansatzes/ucc.py | 2 +- .../library/initial_states/hartree_fock.py | 2 +- .../pes_samplers/test_bopes_sampler.py | 1 - test/circuit/library/ansatzes/test_puccd.py | 24 +-- test/circuit/library/ansatzes/test_succd.py | 36 ++-- test/circuit/library/ansatzes/test_ucc.py | 33 ++-- test/circuit/library/ansatzes/test_uccsd.py | 98 +++++----- .../test_qubit_converter.py | 11 +- .../second_quantization/test_bksf_mapper.py | 9 +- .../second_quantization/test_fermionic_op.py | 175 +++++++++++------- 10 files changed, 223 insertions(+), 168 deletions(-) diff --git a/qiskit_nature/circuit/library/ansatzes/ucc.py b/qiskit_nature/circuit/library/ansatzes/ucc.py index 4c3cac2852..037ff0baa4 100644 --- a/qiskit_nature/circuit/library/ansatzes/ucc.py +++ b/qiskit_nature/circuit/library/ansatzes/ucc.py @@ -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`) diff --git a/qiskit_nature/circuit/library/initial_states/hartree_fock.py b/qiskit_nature/circuit/library/initial_states/hartree_fock.py index 994d1efaa0..ceacf31d5f 100644 --- a/qiskit_nature/circuit/library/initial_states/hartree_fock.py +++ b/qiskit_nature/circuit/library/initial_states/hartree_fock.py @@ -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) diff --git a/test/algorithms/pes_samplers/test_bopes_sampler.py b/test/algorithms/pes_samplers/test_bopes_sampler.py index a1971841b4..eeb92a1859 100644 --- a/test/algorithms/pes_samplers/test_bopes_sampler.py +++ b/test/algorithms/pes_samplers/test_bopes_sampler.py @@ -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) diff --git a/test/circuit/library/ansatzes/test_puccd.py b/test/circuit/library/ansatzes/test_puccd.py index 834c2caba8..f998be0569 100644 --- a/test/circuit/library/ansatzes/test_puccd.py +++ b/test/circuit/library/ansatzes/test_puccd.py @@ -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"), ], ), ) @@ -61,9 +61,9 @@ 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"), ], ), ( @@ -71,8 +71,8 @@ def test_puccd_ansatz(self, num_spin_orbitals, num_particles, expect): (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"), ], ), ( @@ -80,8 +80,8 @@ def test_puccd_ansatz(self, num_spin_orbitals, num_particles, expect): (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"), ], ), ) diff --git a/test/circuit/library/ansatzes/test_succd.py b/test/circuit/library/ansatzes/test_succd.py index eb45aa56cf..6d67cc1d77 100644 --- a/test/circuit/library/ansatzes/test_succd.py +++ b/test/circuit/library/ansatzes/test_succd.py @@ -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"), ], ), ) @@ -67,9 +67,9 @@ 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"), ], ), ( @@ -77,8 +77,8 @@ def test_succd_ansatz(self, num_spin_orbitals, num_particles, expect): (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"), ], ), ( @@ -86,8 +86,8 @@ def test_succd_ansatz(self, num_spin_orbitals, num_particles, expect): (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"), ], ), ) diff --git a/test/circuit/library/ansatzes/test_ucc.py b/test/circuit/library/ansatzes/test_ucc.py index afca214283..43150863c6 100644 --- a/test/circuit/library/ansatzes/test_ucc.py +++ b/test/circuit/library/ansatzes/test_ucc.py @@ -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): @@ -48,14 +48,14 @@ 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"), ], ), ( @@ -63,12 +63,17 @@ class TestUCC(QiskitNatureTestCase): 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): diff --git a/test/circuit/library/ansatzes/test_uccsd.py b/test/circuit/library/ansatzes/test_uccsd.py index eef7872a4b..3625a6f512 100644 --- a/test/circuit/library/ansatzes/test_uccsd.py +++ b/test/circuit/library/ansatzes/test_uccsd.py @@ -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"), ], ), ) diff --git a/test/converters/second_quantization/test_qubit_converter.py b/test/converters/second_quantization/test_qubit_converter.py index 4468362259..ac44002b23 100644 --- a/test/converters/second_quantization/test_qubit_converter.py +++ b/test/converters/second_quantization/test_qubit_converter.py @@ -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 @@ -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) diff --git a/test/mappers/second_quantization/test_bksf_mapper.py b/test/mappers/second_quantization/test_bksf_mapper.py index 83374c1e90..7ee0640350 100644 --- a/test/mappers/second_quantization/test_bksf_mapper.py +++ b/test/mappers/second_quantization/test_bksf_mapper.py @@ -94,12 +94,14 @@ def test_h2(self): """Test H2 molecule""" with self.subTest("Excitation edges 1"): assert np.alltrue( - bksf._bksf_edge_list_fermionic_op(FermionicOp("+-+-")) == np.array([[0, 1], [2, 3]]) + bksf._bksf_edge_list_fermionic_op(FermionicOp("+-+-", display_format="dense")) + == np.array([[0, 1], [2, 3]]) ) with self.subTest("Excitation edges 2"): assert np.alltrue( - bksf._bksf_edge_list_fermionic_op(FermionicOp("+--+")) == np.array([[0, 1], [3, 2]]) + bksf._bksf_edge_list_fermionic_op(FermionicOp("+--+", display_format="dense")) + == np.array([[0, 1], [3, 2]]) ) ## H2 from pyscf with sto-3g basis @@ -119,7 +121,8 @@ def test_h2(self): ("NIIN", (0.6634680964235684 + 0j)), ("NINI", (0.6744887663568382 + 0j)), ("NNII", (0.48217928821207245 + 0j)), - ] + ], + display_format="dense", ) expected_pauli_op = SparsePauliOp.from_list( diff --git a/test/operators/second_quantization/test_fermionic_op.py b/test/operators/second_quantization/test_fermionic_op.py index 3b99be5f31..e3edfe4488 100644 --- a/test/operators/second_quantization/test_fermionic_op.py +++ b/test/operators/second_quantization/test_fermionic_op.py @@ -13,6 +13,7 @@ """Test for FermionicOp""" import unittest +import warnings from functools import lru_cache from itertools import product from test import QiskitNatureTestCase @@ -59,7 +60,7 @@ def assertFermionEqual(self, first: FermionicOp, second: FermionicOp): @unpack def test_init(self, label, pre_processing): """Test __init__""" - fer_op = FermionicOp(pre_processing(label)) + fer_op = FermionicOp(pre_processing(label), display_format="dense") self.assertListEqual(fer_op.to_list(), [(label, 1)]) self.assertFermionEqual(eval(repr(fer_op)), fer_op) # pylint: disable=eval-used @@ -77,8 +78,10 @@ def test_init(self, label, pre_processing): def test_init_sparse_label(self, labels, pre_processing): """Test __init__ with sparse label""" dense_label, sparse_label = labels - fer_op = FermionicOp(pre_processing(sparse_label), register_length=len(dense_label)) - targ = FermionicOp(dense_label) + fer_op = FermionicOp( + pre_processing(sparse_label), register_length=len(dense_label), display_format="sparse" + ) + targ = FermionicOp(dense_label, display_format="sparse") self.assertFermionEqual(fer_op, targ) @data( @@ -89,22 +92,26 @@ def test_init_sparse_label(self, labels, pre_processing): def test_init_invalid_label(self, label, register_length): """Test __init__ with invalid label""" with self.assertRaises(ValueError): - FermionicOp(label, register_length=register_length) + FermionicOp(label, register_length=register_length, display_format="dense") def test_init_multiterm(self): """Test __init__ with multi terms""" with self.subTest("Test 1"): labels = [("N", 2), ("-", 3.14)] - self.assertListEqual(FermionicOp(labels).to_list(), labels) + self.assertListEqual(FermionicOp(labels, display_format="dense").to_list(), labels) with self.subTest("Test 2"): labels = [("+-", 1), ("-+", -1)] - op = FermionicOp([("+_0 -_1", 1.0), ("-_0 +_1", -1.0)], register_length=2) + op = FermionicOp( + [("+_0 -_1", 1.0), ("-_0 +_1", -1.0)], register_length=2, display_format="dense" + ) self.assertListEqual(op.to_list(), labels) def test_init_multiple_digits(self): """Test __init__ for sparse label with multiple digits""" - actual = FermionicOp([("-_2 +_10", 1 + 2j), ("-_12", 56)], register_length=13) + actual = FermionicOp( + [("-_2 +_10", 1 + 2j), ("-_12", 56)], register_length=13, display_format="dense" + ) desired = [ ("II-IIIIIII+II", 1 + 2j), ("IIIIIIIIIIII-", 56), @@ -114,55 +121,65 @@ def test_init_multiple_digits(self): @data(str2str, str2tuple, str2list) def test_init_empty_str(self, pre_processing): """Test __init__ with empty string""" - actual = FermionicOp(pre_processing(""), register_length=3) - desired = FermionicOp("III") + actual = FermionicOp(pre_processing(""), register_length=3, display_format="dense") + desired = FermionicOp("III", display_format="dense") self.assertFermionEqual(actual, desired) def test_neg(self): """Test __neg__""" - fer_op = -FermionicOp("+N-EII") - targ = FermionicOp([("+N-EII", -1)]) + fer_op = -FermionicOp("+N-EII", display_format="dense") + targ = FermionicOp([("+N-EII", -1)], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_mul(self): """Test __mul__, and __rmul__""" with self.subTest("rightmul"): - fer_op = FermionicOp("+-") * 2 - targ = FermionicOp([("+-", 2)]) + fer_op = FermionicOp("+-", display_format="dense") * 2 + targ = FermionicOp([("+-", 2)], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("left mul"): - fer_op = (2 + 1j) * FermionicOp([("+N", 3), ("E-", 1)]) - targ = FermionicOp([("+N", (6 + 3j)), ("E-", (2 + 1j))]) + fer_op = (2 + 1j) * FermionicOp([("+N", 3), ("E-", 1)], display_format="dense") + targ = FermionicOp([("+N", (6 + 3j)), ("E-", (2 + 1j))], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_div(self): """Test __truediv__""" - fer_op = FermionicOp([("+N", 3), ("E-", 1)]) / 3 - targ = FermionicOp([("+N", 1.0), ("E-", 1 / 3)]) + fer_op = FermionicOp([("+N", 3), ("E-", 1)], display_format="dense") / 3 + targ = FermionicOp([("+N", 1.0), ("E-", 1 / 3)], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_add(self): """Test __add__""" - fer_op = 3 * FermionicOp("+N") + FermionicOp("E-") - targ = FermionicOp([("+N", 3), ("E-", 1)]) + fer_op = 3 * FermionicOp("+N", display_format="dense") + FermionicOp( + "E-", display_format="dense" + ) + targ = FermionicOp([("+N", 3), ("E-", 1)], display_format="dense") self.assertFermionEqual(fer_op, targ) - fer_op = sum(FermionicOp(label) for label in ["NIII", "INII", "IINI", "IIIN"]) - targ = FermionicOp([("NIII", 1), ("INII", 1), ("IINI", 1), ("IIIN", 1)]) + fer_op = sum( + FermionicOp(label, display_format="dense") for label in ["NIII", "INII", "IINI", "IIIN"] + ) + targ = FermionicOp( + [("NIII", 1), ("INII", 1), ("IINI", 1), ("IIIN", 1)], display_format="dense" + ) self.assertFermionEqual(fer_op, targ) def test_sub(self): """Test __sub__""" - fer_op = 3 * FermionicOp("++") - 2 * FermionicOp("--") - targ = FermionicOp([("++", 3), ("--", -2)]) + fer_op = 3 * FermionicOp("++", display_format="dense") - 2 * FermionicOp( + "--", display_format="dense" + ) + targ = FermionicOp([("++", 3), ("--", -2)], display_format="dense") self.assertFermionEqual(fer_op, targ) @data(*product(dense_labels(1), dense_labels(1))) @unpack def test_matmul(self, label1, label2): """Test matrix multiplication""" - fer_op = FermionicOp(label1) @ FermionicOp(label2) + fer_op = FermionicOp(label1, display_format="dense") @ FermionicOp( + label2, display_format="dense" + ) mapping = { "II": "I", "I+": "+", @@ -191,70 +208,88 @@ def test_matmul(self, label1, label2): "EE": "E", } result = mapping[label1 + label2] - targ = FermionicOp([(result, 1)] if result != 0 else [("I", 0)]) + targ = FermionicOp([(result, 1)] if result != 0 else [("I", 0)], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_matmul_multi(self): """Test matrix multiplication""" with self.subTest("single matmul"): - fer_op = FermionicOp("+-") @ FermionicOp("-I") - targ = FermionicOp([("N-", -1)]) + fer_op = FermionicOp("+-", display_format="dense") @ FermionicOp( + "-I", display_format="dense" + ) + targ = FermionicOp([("N-", -1)], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("multi matmul"): - fer_op = FermionicOp("+-") @ FermionicOp("-I") - fer_op = (FermionicOp("+N") + FermionicOp("E-")) @ ( - FermionicOp("II") + FermionicOp("-+") + fer_op = FermionicOp("+-", display_format="dense") @ FermionicOp( + "-I", display_format="dense" + ) + fer_op = ( + FermionicOp("+N", display_format="dense") + + FermionicOp("E-", display_format="dense") + ) @ ( + FermionicOp("II", display_format="dense") + + FermionicOp("-+", display_format="dense") + ) + targ = FermionicOp( + [("+N", 1), ("N+", 1), ("E-", 1), ("-E", -1)], display_format="dense" ) - targ = FermionicOp([("+N", 1), ("N+", 1), ("E-", 1), ("-E", -1)]) self.assertFermionEqual(fer_op, targ) def test_pow(self): """Test __pow__""" with self.subTest("square trivial"): - fer_op = FermionicOp([("+N", 3), ("E-", 1)]) ** 2 - targ = FermionicOp([("II", 0)]) + fer_op = FermionicOp([("+N", 3), ("E-", 1)], display_format="dense") ** 2 + targ = FermionicOp([("II", 0)], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("square nontrivial"): - fer_op = FermionicOp([("+N", 3), ("N-", 1)]) ** 2 - targ = FermionicOp([("+-", -3)]) + fer_op = FermionicOp([("+N", 3), ("N-", 1)], display_format="dense") ** 2 + targ = FermionicOp([("+-", -3)], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("3rd power"): - fer_op = (3 * FermionicOp("IIII")) ** 3 - targ = FermionicOp([("IIII", 27)]) + fer_op = (3 * FermionicOp("IIII", display_format="dense")) ** 3 + targ = FermionicOp([("IIII", 27)], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("0th power"): - fer_op = FermionicOp([("+N", 3), ("E-", 1)]) ** 0 - targ = FermionicOp([("II", 1)]) + fer_op = FermionicOp([("+N", 3), ("E-", 1)], display_format="dense") ** 0 + targ = FermionicOp([("II", 1)], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_adjoint(self): """Test adjoint method""" with self.subTest("adjoint"): - fer_op = ~FermionicOp([("+N", 3), ("N-", 1), ("--", 2 + 4j)]) - targ = FermionicOp([("-N", 3), ("N+", 1), ("++", (-2 + 4j))]) + fer_op = ~FermionicOp([("+N", 3), ("N-", 1), ("--", 2 + 4j)], display_format="dense") + targ = FermionicOp([("-N", 3), ("N+", 1), ("++", (-2 + 4j))], display_format="dense") self.assertFermionEqual(fer_op, targ) with self.subTest("adjoint 2"): - fer_op = FermionicOp([("+-", 1), ("II", 2j)]).adjoint() - targ = FermionicOp([("-+", -1), ("II", -2j)]) + fer_op = FermionicOp([("+-", 1), ("II", 2j)], display_format="dense").adjoint() + targ = FermionicOp([("-+", -1), ("II", -2j)], display_format="dense") self.assertFermionEqual(fer_op, targ) def test_reduce(self): """Test reduce""" with self.subTest("reduce integer"): - fer_op = FermionicOp("N") + FermionicOp("E") + FermionicOp("N") + fer_op = ( + FermionicOp("N", display_format="dense") + + FermionicOp("E", display_format="dense") + + FermionicOp("N", display_format="dense") + ) reduced_op = fer_op.reduce() - targ = FermionicOp([("N", 1), ("I", 1)]) + targ = FermionicOp([("N", 1), ("I", 1)], display_format="dense") self.assertFermionEqual(reduced_op, targ) with self.subTest("reduce complex"): - fer_op = FermionicOp("+") + 1j * FermionicOp("-") + 1j * FermionicOp("+") + fer_op = ( + FermionicOp("+", display_format="dense") + + 1j * FermionicOp("-", display_format="dense") + + 1j * FermionicOp("+", display_format="dense") + ) reduced_op = fer_op.reduce() - targ = FermionicOp([("+", 1 + 1j), ("-", 1j)]) + targ = FermionicOp([("+", 1 + 1j), ("-", 1j)], display_format="dense") self.assertFermionEqual(reduced_op, targ) def test_hermiticity(self): @@ -262,21 +297,21 @@ def test_hermiticity(self): with self.subTest("operator hermitian"): # deliberately define test operator with duplicate terms in case .adjoint() simplifies terms fer_op = ( - 1j * FermionicOp("+-NE") - + 1j * FermionicOp("+-NE") - + 1j * FermionicOp("-+NE") - + 1j * FermionicOp("-+NE") - + FermionicOp("+-EN") - - FermionicOp("-+EN") + 1j * FermionicOp("+-NE", display_format="dense") + + 1j * FermionicOp("+-NE", display_format="dense") + + 1j * FermionicOp("-+NE", display_format="dense") + + 1j * FermionicOp("-+NE", display_format="dense") + + FermionicOp("+-EN", display_format="dense") + - FermionicOp("-+EN", display_format="dense") ) self.assertTrue(fer_op.is_hermitian()) with self.subTest("operator not hermitian"): fer_op = ( - 1j * FermionicOp("+-NE") - + 1j * FermionicOp("+-NE") - - 1j * FermionicOp("-+NE") - - 1j * FermionicOp("-+NE") + 1j * FermionicOp("+-NE", display_format="dense") + + 1j * FermionicOp("+-NE", display_format="dense") + - 1j * FermionicOp("-+NE", display_format="dense") + - 1j * FermionicOp("-+NE", display_format="dense") ) self.assertFalse(fer_op.is_hermitian()) @@ -293,7 +328,7 @@ def test_hermiticity(self): @unpack def test_label_display_mode(self, label, pre_processing): """test label_display_mode""" - fer_op = FermionicOp(pre_processing(label)) + fer_op = FermionicOp(pre_processing(label), display_format="dense") fer_op.display_format = "sparse" self.assertListEqual(fer_op.to_list(), str2list(label)) @@ -303,42 +338,54 @@ def test_label_display_mode(self, label, pre_processing): def test_normal_order(self): """test normal_order method""" with self.subTest("Test for creation operator"): - orig = FermionicOp("+") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = FermionicOp("+") fer_op = orig.to_normal_order() targ = FermionicOp("+_0", display_format="sparse") self.assertFermionEqual(fer_op, targ) self.assertEqual(orig.display_format, "dense") with self.subTest("Test for annihilation operator"): - orig = FermionicOp("-") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = FermionicOp("-") fer_op = orig.to_normal_order() targ = FermionicOp("-_0", display_format="sparse") self.assertFermionEqual(fer_op, targ) self.assertEqual(orig.display_format, "dense") with self.subTest("Test for number operator"): - orig = FermionicOp("N") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = FermionicOp("N") fer_op = orig.to_normal_order() targ = FermionicOp("+_0 -_0", display_format="sparse") self.assertFermionEqual(fer_op, targ) self.assertEqual(orig.display_format, "dense") with self.subTest("Test for empty operator"): - orig = FermionicOp("E") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = FermionicOp("E") fer_op = orig.to_normal_order() targ = FermionicOp([("", 1), ("+_0 -_0", -1)], display_format="sparse") self.assertFermionEqual(fer_op, targ) self.assertEqual(orig.display_format, "dense") with self.subTest("Test for multiple operators 1"): - orig = FermionicOp("-+") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = FermionicOp("-+") fer_op = orig.to_normal_order() targ = FermionicOp([("+_1 -_0", -1)], display_format="sparse") self.assertFermionEqual(fer_op, targ) self.assertEqual(orig.display_format, "dense") with self.subTest("Test for multiple operators 2"): - orig = 3 * FermionicOp("E+-") + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=UserWarning) + orig = 3 * FermionicOp("E+-") fer_op = orig.to_normal_order() targ = FermionicOp([("+_1 -_2", 3), ("+_0 +_1 -_0 -_2", 3)], display_format="sparse") self.assertFermionEqual(fer_op, targ)