0
$\begingroup$

I am running a standard VQE from the example shown in here https://qiskit.org/textbook/ch-applications/vqe-molecules.html, however I keep getting this output constantly (as many times the vqe runs which is a lot).

DeprecationWarning: The Python built-in `round` is deprecated for complex scalars, 
and will raise a `TypeError` in a future release. Use `np.round` or `scalar.round` 
instead.

is there any way to suppress this without suppressing the entire python terminal as I need the output for other things.

$\endgroup$

2 Answers 2

1
$\begingroup$

You can add the following before running the VQE to suppress the deprecation warning

import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
# run VQE here

That turns all the deprecation warnings off, if you want to turn them on again you can add

warnings.filterwarnings('always', category=DeprecationWarning)

I don't think there is a way to turn off just Qiskit's deprecation warnings.

$\endgroup$
2
  • $\begingroup$ Just what im looking for, thanks! $\endgroup$ Aug 5, 2020 at 13:48
  • $\begingroup$ Great! Could you accept the answer so others see it has been addressed? $\endgroup$
    – Cryoris
    Aug 5, 2020 at 13:54
1
$\begingroup$

@Cryoris answer is perfectly valid, but a more "Pythonic" way of doing this is with the help of the with keyword:

import warnings

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=DeprecationWarning) 
    # Run VQE here, respect the identation.
# /!\ At this level of identation, warnings are no longer ignored.
# No need to think to call another method afterwards.

The issue with this code being that you ignore all the deprecation warnings. If you want to only ignore the one that bother you, add a filter:

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore", 
                          category=DeprecationWarning, 
                          message=(
                                 "The Python built-in round is deprecated for complex "
                                 "scalars, and will raise a TypeError in a future release. "
                                 "Use np.round or scalar.round instead."
                             ) 
                         ) 
    # Run VQE here, respect the identation.
# /!\ At this level of identation, warnings are no longer ignored.
# No need to think to call another method afterwards.

This answer has been constructed from this other answer and the warnings module documentation.

I tested with

import warnings
def f():
    warnings.warn("The Python built-in round is deprecated for complex scalars, and will raise a TypeError in a future release. Use np.round or scalar.round instead.", DeprecationWarning)
    
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", 
                          category=DeprecationWarning, 
                          message=(
                                 "The Python built-in round is deprecated for complex "
                                 "scalars, and will raise a TypeError in a future release. "
                                 "Use np.round or scalar.round instead."
                             ) 
                         ) 
    print("In 'with' block:")
    f()
    print("Done")
print("Out of 'with' block:")
f()
print("Done")

that prints

In 'with' block:
Done
Out of 'with' block:
-c:2: DeprecationWarning: The Python built-in round is deprecated for complex scalars, and will raise a TypeError in a future release. Use np.round or scalar.round instead.
Done

in my IPython session. You can check that the warning has been filtered within the with block and not filtered outside of it.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.