Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cirq-core/cirq/circuits/circuit_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
4 changes: 3 additions & 1 deletion cirq-core/cirq/contrib/qasm_import/_parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/experiments/xeb_simulation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
14 changes: 7 additions & 7 deletions cirq-core/cirq/ops/clifford_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -237,26 +237,26 @@ 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

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

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

Expand Down
9 changes: 6 additions & 3 deletions cirq-google/cirq_google/devices/google_noise_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
6 changes: 5 additions & 1 deletion cirq-google/cirq_google/ops/fsim_gate_family_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:],
Expand Down
19 changes: 10 additions & 9 deletions cirq-rigetti/cirq_rigetti/quil_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions cirq-web/cirq_web/bloch_sphere/bloch_sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"""
Expand Down
36 changes: 21 additions & 15 deletions cirq-web/cirq_web/bloch_sphere/bloch_sphere_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)


Expand All @@ -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"""
<script>
Expand All @@ -70,8 +63,21 @@ def test_bloch_sphere_default_client_code():
assert expected_client_code == bloch_sphere.get_client_code()


def test_bloch_sphere_default_client_code_comp_basis():
bloch_sphere = cirq_web.BlochSphere(state_vector=1)

expected_client_code = f"""
<script>
renderBlochSphere('{bloch_sphere.id}', 5)
.addVector(0.0, 0.0, -1.0);
</script>
"""

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'