Skip to content

Commit

Permalink
Merge branch 'main' into z_cal2
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Sep 24, 2024
2 parents 6f71abf + 756abe3 commit 87cb95a
Show file tree
Hide file tree
Showing 25 changed files with 503 additions and 237 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ jobs:
run: check/pytest -n auto --durations=20 --ignore=cirq-core/cirq/contrib --rigetti-integration
- name: Stop Quil dependencies
run: docker compose -f cirq-rigetti/docker-compose.test.yaml down
# TODO(#6706) remove after we start using NumPy 2.0 in regular pytest
pytest-numpy-2:
name: Pytest Ubuntu with NumPy-2
strategy:
matrix:
python-version: ['3.10', '3.11', '3.12']
runs-on: ubuntu-20.04
steps:
- name: Check out source repository
uses: actions/checkout@v4
- name: Set up Python environment
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: 'x64'
- name: Set up caching of dependencies
uses: actions/cache@v4
with:
path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('**/requirements.txt', 'dev_tools/requirements/**/*.txt') }}
- name: Install requirements
run: |
pip install wheel
pip install --upgrade --upgrade-strategy eager -r dev_tools/requirements/dev-np2.env.txt
- name: Pytest check
run: check/pytest -n auto --warn-numpy-data-promotion --durations=20 --ignore=cirq-rigetti
pip-compile:
name: Check consistency of requirements
runs-on: ubuntu-20.04
Expand Down
3 changes: 3 additions & 0 deletions cirq-core/cirq/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def _print(self, expr, **kwargs):
if hasattr(value, "__qualname__"):
return f"{value.__module__}.{value.__qualname__}"

if isinstance(value, np.number):
return str(value)

return repr(value)


Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/contrib/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ ply>=3.6
pylatex~=1.4

# quimb
quimb~=1.7
quimb>=1.8
opt_einsum
2 changes: 2 additions & 0 deletions cirq-core/cirq/protocols/circuit_diagram_info_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ def format_radians(self, radians: Union[sympy.Basic, int, float]) -> str:
if self.precision is not None and not isinstance(radians, sympy.Basic):
quantity = self.format_real(radians / np.pi)
return quantity + unit
if isinstance(radians, np.number):
return str(radians)
return repr(radians)

def copy(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def test_format_real():
assert args.format_real(sympy.Symbol('t')) == 't'
assert args.format_real(sympy.Symbol('t') * 2 + 1) == '2*t + 1'

assert args.format_real(np.float64(1.1)) == '1.1'
assert args.format_real(np.int32(1)) == '1'

args.precision = None
assert args.format_real(1) == '1'
assert args.format_real(1.1) == '1.1'
Expand Down Expand Up @@ -252,6 +255,7 @@ def test_format_radians_without_precision():
assert args.format_radians(-np.pi) == '-pi'
assert args.format_radians(1.1) == '1.1'
assert args.format_radians(1.234567) == '1.234567'
assert args.format_radians(np.float32(1.234567)) == '1.234567'
assert args.format_radians(1 / 7) == repr(1 / 7)
assert args.format_radians(sympy.Symbol('t')) == 't'
assert args.format_radians(sympy.Symbol('t') * 2 + 1) == '2*t + 1'
Expand All @@ -261,6 +265,7 @@ def test_format_radians_without_precision():
assert args.format_radians(-np.pi) == '-π'
assert args.format_radians(1.1) == '1.1'
assert args.format_radians(1.234567) == '1.234567'
assert args.format_radians(np.float32(1.234567)) == '1.234567'
assert args.format_radians(1 / 7) == repr(1 / 7)
assert args.format_radians(sympy.Symbol('t')) == 't'
assert args.format_radians(sympy.Symbol('t') * 2 + 1) == '2*t + 1'
Expand Down
7 changes: 7 additions & 0 deletions cirq-core/cirq/protocols/json_serialization_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class _ModuleDeprecation:
'non_existent_should_be_fine': None,
}

# TODO(#6706) remove after cirq_rigetti supports NumPy 2.0
if np.__version__.startswith("2."): # pragma: no cover
warnings.warn(
"json_serialization_test - ignoring cirq_rigetti due to incompatibility with NumPy 2.0"
)
del TESTED_MODULES["cirq_rigetti"]


def _get_testspecs_for_modules() -> List[ModuleJsonTestSpec]:
modules = []
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/qis/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def quantum_state(
dtype = DEFAULT_COMPLEX_DTYPE
data = one_hot(index=state, shape=(dim,), dtype=dtype)
else:
data = np.array(state, copy=False)
data = np.asarray(state)
if qid_shape is None:
qid_shape = infer_qid_shape(state)
if data.ndim == 1 and data.dtype.kind != 'c':
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def __repr__(self) -> str:
def __str__(self) -> str:
def bitstring(vals):
separator = ' ' if np.max(vals) >= 10 else ''
return separator.join(str(int(v)) for v in vals)
return separator.join(str(v.item()) for v in vals)

results = sorted([(key, bitstring(val)) for key, val in self.measurements.items()])
if not results:
Expand Down
8 changes: 4 additions & 4 deletions cirq-core/cirq/sim/sparse_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_run_repetitions_terminal_measurement_stochastic():
q = cirq.LineQubit(0)
c = cirq.Circuit(cirq.H(q), cirq.measure(q, key='q'))
results = cirq.Simulator().run(c, repetitions=10000)
assert 1000 <= sum(v[0] for v in results.measurements['q']) < 9000
assert 1000 <= np.count_nonzero(results.measurements['q']) < 9000


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_run_mixture(dtype: Type[np.complexfloating], split: bool):
simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split)
circuit = cirq.Circuit(cirq.bit_flip(0.5)(q0), cirq.measure(q0))
result = simulator.run(circuit, repetitions=100)
assert 20 < sum(result.measurements['q(0)'])[0] < 80
assert 20 < np.count_nonzero(result.measurements['q(0)']) < 80


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
Expand All @@ -265,8 +265,8 @@ def test_run_mixture_with_gates(dtype: Type[np.complexfloating], split: bool):
simulator = cirq.Simulator(dtype=dtype, split_untangled_states=split, seed=23)
circuit = cirq.Circuit(cirq.H(q0), cirq.phase_flip(0.5)(q0), cirq.H(q0), cirq.measure(q0))
result = simulator.run(circuit, repetitions=100)
assert sum(result.measurements['q(0)'])[0] < 80
assert sum(result.measurements['q(0)'])[0] > 20
assert np.count_nonzero(result.measurements['q(0)']) < 80
assert np.count_nonzero(result.measurements['q(0)']) > 20


@pytest.mark.parametrize('dtype', [np.complex64, np.complex128])
Expand Down
Loading

0 comments on commit 87cb95a

Please sign in to comment.