TransWikia.com

Suppress deprecation warnings from Qiskit

Quantum Computing Asked by Akashkumar Patel on May 8, 2021

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.

2 Answers

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.

Correct answer by Cryoris on May 8, 2021

@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.

Answered by Adrien Suau on May 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP