From 2c0c48179f66b2bc4a185131c99e02d9a512e948 Mon Sep 17 00:00:00 2001 From: Balint Pato Date: Thu, 1 Oct 2020 12:59:59 -0400 Subject: [PATCH] Fix quimb version checking (#3378) Making quimb version check more robust. In some systems the version might not be parseable as (int,int), e.g `0+unknown`. This PR fixes these scenarios. Fixes #3263. --- cirq/contrib/quimb/state_vector.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cirq/contrib/quimb/state_vector.py b/cirq/contrib/quimb/state_vector.py index d8069e2697a..831250820eb 100644 --- a/cirq/contrib/quimb/state_vector.py +++ b/cirq/contrib/quimb/state_vector.py @@ -7,7 +7,21 @@ import cirq -QUIMB_VERSION = tuple(int(x) for x in quimb.__version__.split('.')) + +# coverage: ignore +def _get_quimb_version(): + """Returns the quimb version and parsed (major,minor) numbers if possible. + Returns: + a tuple of ((major, minor), version string) + """ + version = quimb.__version__ + try: + return tuple(int(x) for x in version.split('.')), version + except: + return (0, 0), version + + +QUIMB_VERSION = _get_quimb_version() def circuit_to_tensors(circuit: cirq.Circuit, @@ -150,9 +164,10 @@ def tensor_expectation_value(circuit: cirq.Circuit, tags={'Q0', 'bra0'}) for q in qubits ] tn = qtn.TensorNetwork(tensors + end_bras) - if QUIMB_VERSION < (1, 3): + if QUIMB_VERSION[0] < (1, 3): # coverage: ignore - warnings.warn('Please use quimb>=1.3 for optimal performance in ' + warnings.warn(f'quimb version {QUIMB_VERSION[1]} detected. Please use ' + f'quimb>=1.3 for optimal performance in ' '`tensor_expectation_value`. ' 'See https://github.com/quantumlib/Cirq/issues/3263') else: