From 4a6148d4d428c58133fd715dabf2620984536ba9 Mon Sep 17 00:00:00 2001 From: Davide Bincoletto Date: Wed, 11 Dec 2024 12:54:28 +0100 Subject: [PATCH] wfn compact visualization --- src/tequila/wavefunction/qubit_wavefunction.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/tequila/wavefunction/qubit_wavefunction.py b/src/tequila/wavefunction/qubit_wavefunction.py index 1c700d1c..ce40f3a4 100644 --- a/src/tequila/wavefunction/qubit_wavefunction.py +++ b/src/tequila/wavefunction/qubit_wavefunction.py @@ -365,7 +365,7 @@ def normalize(self, inplace: bool = False) -> QubitWaveFunction: # because the __mul__ implementation of the number tries to perform some sort of array # operation. def length(self): - return sum(1 for (k, v) in self.raw_items() if abs(v) > 1e-6) + return sum(1 for _ in self.raw_items()) def __repr__(self): result = str() @@ -373,7 +373,10 @@ def __repr__(self): index = index.integer if self.numbering == BitNumbering.LSB: index = reverse_int_bits(index, self._n_qubits) - result += f"{coeff} |{index:0{self._n_qubits}b}> " + if np.isclose(coeff.imag,0.0): + result += f"{coeff.real:+2.4f} |{index:0{self._n_qubits}b}> " + else: + result += f"({coeff.real:+2.4f} + {coeff.imag:+2.4f}i) |{index:0{self._n_qubits}b}>" # If the wavefunction contains no states if not result: result = "empty wavefunction"