Skip to content

Commit 82a5066

Browse files
round for paulistring
1 parent c806c41 commit 82a5066

File tree

6 files changed

+30
-8
lines changed

6 files changed

+30
-8
lines changed

examples/scripts/google_execution_trials.py

-2
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,3 @@
5656

5757
plot_results_sample_mode(results)
5858
plt.show()
59-
60-
# %%

examples/scripts/observable_paulistring.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
ps_obs = Observable(1 * I @ Z + 1 * I @ I)
2525

26-
obs = ps_obs.to_matrix()
26+
obs = ps_obs.matrix
2727

2828
print(obs)
2929
print(ps_obs)

mpqp/all.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
submit,
1717
)
1818
from .execution.connection.qlm_connection import get_all_job_ids
19-
from .execution.devices import ATOSDevice, AWSDevice, IBMDevice
19+
from .execution.devices import ATOSDevice, AWSDevice, IBMDevice, GOOGLEDevice
2020
from .execution.vqa import Optimizer, minimize
2121
from .gates import (
2222
CNOT,

mpqp/core/instruction/measurement/pauli_string.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def nb_qubits(self) -> int:
6363
return 0 if len(self._monomials) == 0 else self._monomials[0].nb_qubits
6464

6565
def __str__(self):
66-
return " + ".join(map(str, self._monomials))
66+
return " + ".join(map(str, self.round().monomials))
6767

6868
def __repr__(self):
6969
return str(self)
@@ -165,6 +165,32 @@ def simplify(self) -> PauliString:
165165
PauliStringMonomial(0, [I for _ in range(self.nb_qubits)])
166166
)
167167
return res
168+
169+
def round(self, round_off_till: int = 5) -> PauliString:
170+
"""Round the coefficients of the PauliString to a specified number of decimal places.
171+
172+
Example:
173+
>>> ps = 0.6875*I@I + 0.415*I@X + 0.1275*I@Z + 1.0*X@I + 1.0*X@X + 0.0375*Z@I + 0.085*Z@X + -0.2225*Z@Z
174+
>>> rounded_ps = ps.round(1)
175+
>>> print(rounded_ps)
176+
-0.2*Z@Z + 1.0*X@X + 0.1*I@Z + 1.0*X@I + 0.1*Z@X + 0.7*I@I + 0.4*I@X
177+
178+
Args:
179+
round_off_till : Number of decimal places to round the coefficients to. Defaults to 5.
180+
181+
Returns:
182+
PauliString: A PauliString with coefficients rounded to the specified number of decimal places.
183+
"""
184+
res = PauliString()
185+
for mono in self.monomials:
186+
coef = float(round(mono.coef, round_off_till))
187+
if coef != 0:
188+
res.monomials.append(PauliStringMonomial(coef, mono.atoms))
189+
if len(res.monomials) == 0:
190+
res.monomials.append(
191+
PauliStringMonomial(0, [I for _ in range(self.nb_qubits)])
192+
)
193+
return res
168194

169195
def to_matrix(self) -> Matrix:
170196
"""Convert the PauliString to a matrix representation.

mpqp/execution/providers/google.py

-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
from cirq.sim.state_vector_simulator import StateVectorTrialResult
1818
from cirq.transformers.optimize_for_target_gateset import optimize_for_target_gateset
1919
from cirq.value.probability import state_vector_to_probabilities
20-
from cirq.transformers.routing.route_circuit_cqc import RouteCQC
2120
from cirq.sim.sparse_simulator import Simulator
2221
from cirq.circuits.circuit import Circuit as Cirq_circuit
2322
from cirq.study.result import Result as cirq_result
2423
from cirq.ops.linear_combinations import PauliSum as Cirq_PauliSum
25-
from cirq.transformers.target_gatesets.sqrt_iswap_gateset import SqrtIswapTargetGateset
2624
from cirq_google.engine.virtual_engine_factory import (
2725
load_median_device_calibration,
2826
create_device_from_processor_id,

mpqp/tools/visualization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _prep_plot(result: Result):
5454
plt.xlabel("State")
5555
plt.ylabel("Counts")
5656
device = result.job.device
57-
plt.title(type(device).__name__ + ", " + device.name)
57+
plt.title(type(device).__name__ + "\n" + device.name)
5858

5959

6060
@typechecked

0 commit comments

Comments
 (0)