diff --git a/cirq-core/cirq/circuits/circuit_operation.py b/cirq-core/cirq/circuits/circuit_operation.py index 7888b9348e9..fe6f9b3b22d 100644 --- a/cirq-core/cirq/circuits/circuit_operation.py +++ b/cirq-core/cirq/circuits/circuit_operation.py @@ -50,7 +50,8 @@ def default_repetition_ids(repetitions: IntParam) -> Optional[List[str]]: if isinstance(repetitions, INT_CLASSES) and abs(repetitions) != 1: - return [str(i) for i in range(abs(repetitions))] + abs_repetitions: int = abs(int(repetitions)) + return [str(i) for i in range(abs_repetitions)] return None diff --git a/cirq-core/cirq/contrib/qasm_import/_parser_test.py b/cirq-core/cirq/contrib/qasm_import/_parser_test.py index ddd71ce9220..19cb839dadb 100644 --- a/cirq-core/cirq/contrib/qasm_import/_parser_test.py +++ b/cirq-core/cirq/contrib/qasm_import/_parser_test.py @@ -11,11 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from typing import Callable import numpy as np import pytest import sympy + import cirq import cirq.testing as ct from cirq import Circuit @@ -429,7 +431,7 @@ def test_unknown_function(): @pytest.mark.parametrize('qasm_gate,cirq_gate', rotation_gates) -def test_rotation_gates(qasm_gate: str, cirq_gate: cirq.Gate): +def test_rotation_gates(qasm_gate: str, cirq_gate: Callable[[float], cirq.Gate]): qasm = """OPENQASM 2.0; include "qelib1.inc"; qreg q[2]; diff --git a/cirq-core/cirq/experiments/xeb_simulation_test.py b/cirq-core/cirq/experiments/xeb_simulation_test.py index 8615dd41904..9f5253b8f58 100644 --- a/cirq-core/cirq/experiments/xeb_simulation_test.py +++ b/cirq-core/cirq/experiments/xeb_simulation_test.py @@ -79,8 +79,8 @@ def _ref_simulate_2q_xeb_circuit(task: Dict[str, Any]): pure_sim = cirq.Simulator() psi = pure_sim.simulate(tcircuit) - psi = psi.final_state_vector - pure_probs = cirq.state_vector_to_probabilities(psi) + psi_vector = psi.final_state_vector + pure_probs = cirq.state_vector_to_probabilities(psi_vector) return {'circuit_i': circuit_i, 'cycle_depth': cycle_depth, 'pure_probs': pure_probs} diff --git a/cirq-core/cirq/ops/clifford_gate.py b/cirq-core/cirq/ops/clifford_gate.py index b3c303bd473..5c359a0d337 100644 --- a/cirq-core/cirq/ops/clifford_gate.py +++ b/cirq-core/cirq/ops/clifford_gate.py @@ -79,7 +79,7 @@ def _to_clifford_tableau( clifford_tableau.xs[1, 0] = z_to[0] in (pauli_gates.X, pauli_gates.Y) clifford_tableau.zs[1, 0] = z_to[0] in (pauli_gates.Y, pauli_gates.Z) - clifford_tableau.rs = (x_to[1], z_to[1]) + clifford_tableau.rs = np.array([x_to[1], z_to[1]]) return clifford_tableau @@ -237,8 +237,8 @@ def S(cls) -> 'cirq.SingleQubitCliffordGate': def CNOT(cls) -> 'cirq.CliffordGate': if not hasattr(cls, '_CNOT'): t = qis.CliffordTableau(num_qubits=2) - t.xs = [[1, 1], [0, 1], [0, 0], [0, 0]] - t.zs = [[0, 0], [0, 0], [1, 0], [1, 1]] + t.xs = np.array([[1, 1], [0, 1], [0, 0], [0, 0]]) + t.zs = np.array([[0, 0], [0, 0], [1, 0], [1, 1]]) cls._CNOT = CliffordGate.from_clifford_tableau(t) return cls._CNOT @@ -246,8 +246,8 @@ def CNOT(cls) -> 'cirq.CliffordGate': def CZ(cls) -> 'cirq.CliffordGate': if not hasattr(cls, '_CZ'): t = qis.CliffordTableau(num_qubits=2) - t.xs = [[1, 0], [0, 1], [0, 0], [0, 0]] - t.zs = [[0, 1], [1, 0], [1, 0], [0, 1]] + t.xs = np.array([[1, 0], [0, 1], [0, 0], [0, 0]]) + t.zs = np.array([[0, 1], [1, 0], [1, 0], [0, 1]]) cls._CZ = CliffordGate.from_clifford_tableau(t) return cls._CZ @@ -255,8 +255,8 @@ def CZ(cls) -> 'cirq.CliffordGate': def SWAP(cls) -> 'cirq.CliffordGate': if not hasattr(cls, '_SWAP'): t = qis.CliffordTableau(num_qubits=2) - t.xs = [[0, 1], [1, 0], [0, 0], [0, 0]] - t.zs = [[0, 0], [0, 0], [0, 1], [1, 0]] + t.xs = np.array([[0, 1], [1, 0], [0, 0], [0, 0]]) + t.zs = np.array([[0, 0], [0, 0], [0, 1], [1, 0]]) cls._SWAP = CliffordGate.from_clifford_tableau(t) return cls._SWAP diff --git a/cirq-google/cirq_google/devices/google_noise_properties.py b/cirq-google/cirq_google/devices/google_noise_properties.py index 820f9db45fc..3b5cfb723da 100644 --- a/cirq-google/cirq_google/devices/google_noise_properties.py +++ b/cirq-google/cirq_google/devices/google_noise_properties.py @@ -146,10 +146,13 @@ def with_params( replace_args['tphi_ns'] = _with_values(self.tphi_ns, tphi_ns) if readout_errors is not None: if isinstance(readout_errors, dict): - readout_errors = {k: np.array(v) for k, v in readout_errors.items()} + replace_args['readout_errors'] = _with_values( + self.readout_errors, {k: np.array(v) for k, v in readout_errors.items()} + ) else: - readout_errors = np.array(readout_errors) - replace_args['readout_errors'] = _with_values(self.readout_errors, readout_errors) + replace_args['readout_errors'] = _with_values( + self.readout_errors, np.array(readout_errors) + ) if gate_pauli_errors is not None: if isinstance(gate_pauli_errors, dict): combined_pauli_errors: Dict[ diff --git a/cirq-google/cirq_google/engine/calibration_to_noise_properties.py b/cirq-google/cirq_google/engine/calibration_to_noise_properties.py index 31c1f88d3ff..1263d00b82c 100644 --- a/cirq-google/cirq_google/engine/calibration_to_noise_properties.py +++ b/cirq-google/cirq_google/engine/calibration_to_noise_properties.py @@ -26,7 +26,6 @@ """ from typing import Dict, Optional, Tuple, Type, TYPE_CHECKING -import numpy as np from cirq import ops from cirq.devices import noise_utils @@ -165,9 +164,7 @@ def noise_properties_from_calibration( # 4. Extract readout fidelity for all qubits. p00 = _unpack_1q_from_calibration('single_qubit_p00_error', calibration) p11 = _unpack_1q_from_calibration('single_qubit_p11_error', calibration) - readout_errors = { - q: np.array([p00.get(q, 0), p11.get(q, 0)]) for q in set(p00.keys()) | set(p11.keys()) - } + readout_errors = {q: [p00.get(q, 0), p11.get(q, 0)] for q in set(p00.keys()) | set(p11.keys())} # 5. Extract entangling angle errors. fsim_errors = {} diff --git a/cirq-google/cirq_google/ops/fsim_gate_family_test.py b/cirq-google/cirq_google/ops/fsim_gate_family_test.py index 95528bc8e85..b3adaa1b0c5 100644 --- a/cirq-google/cirq_google/ops/fsim_gate_family_test.py +++ b/cirq-google/cirq_google/ops/fsim_gate_family_test.py @@ -14,6 +14,8 @@ """Tests for Common Gate Families used in cirq-google""" +from typing import List, Union + import pytest import sympy import numpy as np @@ -79,11 +81,13 @@ *VALID_IDENTITY, ] +P_VALUES: List[Union[float, sympy.Expr]] = [np.pi / 4, 0.01, THETA, PHI] + VALID_PHASED_ISWAP_GATES = [ (cirq.PhasedISwapPowGate(exponent=0.1, phase_exponent=PHI), {PHI: 0.24}), *[ (cirq.PhasedFSimGate.from_fsim_rz(THETA, PHI, (-p, p), (p, -p)), {THETA: tv, PHI: pv}) - for p in [np.pi / 4, 0.01, THETA, PHI] + for p in P_VALUES for tv, pv in [(0.4, 0), (-0.1, 2 * np.pi)] ], *VALID_ISWAP_GATES[1:], diff --git a/cirq-rigetti/cirq_rigetti/quil_output.py b/cirq-rigetti/cirq_rigetti/quil_output.py index 90151b4cdf1..954aa571f34 100644 --- a/cirq-rigetti/cirq_rigetti/quil_output.py +++ b/cirq-rigetti/cirq_rigetti/quil_output.py @@ -223,22 +223,23 @@ def _swappow_gate(op: cirq.Operation, formatter: QuilFormatter) -> str: def _twoqubitdiagonal_gate(op: cirq.Operation, formatter: QuilFormatter) -> Optional[str]: gate = cast(cirq.TwoQubitDiagonalGate, op.gate) - if np.count_nonzero(gate._diag_angles_radians) == 1: - if gate._diag_angles_radians[0] != 0: + diag_angles_radians = np.asarray(gate._diag_angles_radians) + if np.count_nonzero(diag_angles_radians) == 1: + if diag_angles_radians[0] != 0: return formatter.format( - 'CPHASE00({0}) {1} {2}\n', gate._diag_angles_radians[0], op.qubits[0], op.qubits[1] + 'CPHASE00({0}) {1} {2}\n', diag_angles_radians[0], op.qubits[0], op.qubits[1] ) - elif gate._diag_angles_radians[1] != 0: + elif diag_angles_radians[1] != 0: return formatter.format( - 'CPHASE01({0}) {1} {2}\n', gate._diag_angles_radians[1], op.qubits[0], op.qubits[1] + 'CPHASE01({0}) {1} {2}\n', diag_angles_radians[1], op.qubits[0], op.qubits[1] ) - elif gate._diag_angles_radians[2] != 0: + elif diag_angles_radians[2] != 0: return formatter.format( - 'CPHASE10({0}) {1} {2}\n', gate._diag_angles_radians[2], op.qubits[0], op.qubits[1] + 'CPHASE10({0}) {1} {2}\n', diag_angles_radians[2], op.qubits[0], op.qubits[1] ) - elif gate._diag_angles_radians[3] != 0: + elif diag_angles_radians[3] != 0: return formatter.format( - 'CPHASE({0}) {1} {2}\n', gate._diag_angles_radians[3], op.qubits[0], op.qubits[1] + 'CPHASE({0}) {1} {2}\n', diag_angles_radians[3], op.qubits[0], op.qubits[1] ) return None diff --git a/cirq-web/cirq_web/bloch_sphere/bloch_sphere.py b/cirq-web/cirq_web/bloch_sphere/bloch_sphere.py index ba434fcde2d..246fba65b0f 100644 --- a/cirq-web/cirq_web/bloch_sphere/bloch_sphere.py +++ b/cirq-web/cirq_web/bloch_sphere/bloch_sphere.py @@ -11,15 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import cirq from cirq_web import widget -from cirq.qis.states import bloch_vector_from_state_vector -from cirq.qis.states import STATE_VECTOR_LIKE - class BlochSphere(widget.Widget): - def __init__(self, sphere_radius: int = 5, state_vector: STATE_VECTOR_LIKE = None): + def __init__(self, sphere_radius: int = 5, state_vector: cirq.STATE_VECTOR_LIKE = None): """Initializes a BlochSphere. Also initializes it's parent class Widget with the bundle file provided. @@ -40,7 +38,10 @@ def __init__(self, sphere_radius: int = 5, state_vector: STATE_VECTOR_LIKE = Non if state_vector is None: raise ValueError('No state vector given in BlochSphere initialization') - self.bloch_vector = bloch_vector_from_state_vector(state_vector, 0) + + self.bloch_vector = cirq.bloch_vector_from_state_vector( + cirq.to_valid_state_vector(state_vector, num_qubits=1), 0 + ) def get_client_code(self) -> str: return f""" diff --git a/cirq-web/cirq_web/bloch_sphere/bloch_sphere_test.py b/cirq-web/cirq_web/bloch_sphere/bloch_sphere_test.py index 580de0a9ae5..2496f5fb3ba 100644 --- a/cirq-web/cirq_web/bloch_sphere/bloch_sphere_test.py +++ b/cirq-web/cirq_web/bloch_sphere/bloch_sphere_test.py @@ -14,25 +14,21 @@ import math +import cirq import pytest import numpy as np import cirq_web -from cirq.qis import to_valid_state_vector -from cirq.qis.states import bloch_vector_from_state_vector - def test_init_bloch_sphere_type(): - state_vector = to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2]) - bloch_sphere = cirq_web.BlochSphere(state_vector=state_vector) + bloch_sphere = cirq_web.BlochSphere(state_vector=[1 / math.sqrt(2), 1 / math.sqrt(2)]) assert isinstance(bloch_sphere, cirq_web.BlochSphere) @pytest.mark.parametrize('sphere_radius', [5, 0.2, 100]) def test_valid_bloch_sphere_radius(sphere_radius): - state_vector = to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2]) - bloch_sphere = cirq_web.BlochSphere(sphere_radius, state_vector) + bloch_sphere = cirq_web.BlochSphere(sphere_radius, [1 / math.sqrt(2), 1 / math.sqrt(2)]) assert sphere_radius == bloch_sphere.sphere_radius @@ -42,12 +38,10 @@ def test_invalid_bloch_sphere_radius(sphere_radius): cirq_web.BlochSphere(sphere_radius=sphere_radius) -@pytest.mark.parametrize( - 'state_vector', [to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2])] -) -def test_valid_bloch_sphere_vector(state_vector): +def test_valid_bloch_sphere_vector(): + state_vector = np.array([1 / math.sqrt(2), 1 / math.sqrt(2)]) bloch_sphere = cirq_web.BlochSphere(state_vector=state_vector) - bloch_vector = bloch_vector_from_state_vector(state_vector, 0) + bloch_vector = cirq.bloch_vector_from_state_vector(state_vector, 0) assert np.array_equal(bloch_vector, bloch_sphere.bloch_vector) @@ -57,8 +51,7 @@ def test_no_state_vector_given(): def test_bloch_sphere_default_client_code(): - state_vector = to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2]) - bloch_sphere = cirq_web.BlochSphere(state_vector=state_vector) + bloch_sphere = cirq_web.BlochSphere(state_vector=[1 / math.sqrt(2), 1 / math.sqrt(2)]) expected_client_code = f""" + """ + + assert expected_client_code == bloch_sphere.get_client_code() + + def test_bloch_sphere_default_bundle_name(): - state_vector = to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2]) + state_vector = cirq.to_valid_state_vector([math.sqrt(2) / 2, math.sqrt(2) / 2]) bloch_sphere = cirq_web.BlochSphere(state_vector=state_vector) assert bloch_sphere.get_widget_bundle_name() == 'bloch_sphere.bundle.js'