Skip to content

Commit

Permalink
CI - run pylint checks with dependencies installed (#6163)
Browse files Browse the repository at this point in the history
* Install all cirq dependencies for pylint check so 
  pylint can verify arguments in function calls.
* Fix pylint complaints on too-many-function-args by
  using a single shape argument for numpy array.reshape().
* Remove obsolete pin of the dill version.
  The current release dill-0.3.6 seems OK w/r to #5383
  • Loading branch information
pavoljuhas committed Jun 27, 2023
1 parent 228c7e6 commit 6565fc5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -90,7 +90,7 @@ jobs:
python-version: '3.8'
architecture: 'x64'
- name: Install pylint
run: pip install -r dev_tools/requirements/deps/pylint.txt
run: pip install -r dev_tools/requirements/pylint.env.txt
- name: Display version
run: check/pylint --version
- name: Lint
Expand Down
14 changes: 7 additions & 7 deletions cirq-core/cirq/linalg/transformations_test.py
Expand Up @@ -352,20 +352,20 @@ def test_sub_state_vector():
a = np.arange(4) / np.linalg.norm(np.arange(4))
b = (np.arange(8) + 3) / np.linalg.norm(np.arange(8) + 3)
c = (np.arange(16) + 1) / np.linalg.norm(np.arange(16) + 1)
state = np.kron(np.kron(a, b), c).reshape(2, 2, 2, 2, 2, 2, 2, 2, 2)
state = np.kron(np.kron(a, b), c).reshape((2, 2, 2, 2, 2, 2, 2, 2, 2))

assert cirq.equal_up_to_global_phase(cirq.sub_state_vector(a, [0, 1], atol=1e-8), a)
assert cirq.equal_up_to_global_phase(cirq.sub_state_vector(b, [0, 1, 2], atol=1e-8), b)
assert cirq.equal_up_to_global_phase(cirq.sub_state_vector(c, [0, 1, 2, 3], atol=1e-8), c)

assert cirq.equal_up_to_global_phase(
cirq.sub_state_vector(state, [0, 1], atol=1e-15), a.reshape(2, 2)
cirq.sub_state_vector(state, [0, 1], atol=1e-15), a.reshape((2, 2))
)
assert cirq.equal_up_to_global_phase(
cirq.sub_state_vector(state, [2, 3, 4], atol=1e-15), b.reshape(2, 2, 2)
cirq.sub_state_vector(state, [2, 3, 4], atol=1e-15), b.reshape((2, 2, 2))
)
assert cirq.equal_up_to_global_phase(
cirq.sub_state_vector(state, [5, 6, 7, 8], atol=1e-15), c.reshape(2, 2, 2, 2)
cirq.sub_state_vector(state, [5, 6, 7, 8], atol=1e-15), c.reshape((2, 2, 2, 2))
)

# Output state vector conforms to the shape of the input state vector.
Expand Down Expand Up @@ -485,15 +485,15 @@ def test_partial_trace_of_state_vector_as_mixture_pure_result():

assert mixtures_equal(
cirq.partial_trace_of_state_vector_as_mixture(state, [0, 1], atol=1e-8),
((1.0, a.reshape(2, 2)),),
((1.0, a.reshape((2, 2))),),
)
assert mixtures_equal(
cirq.partial_trace_of_state_vector_as_mixture(state, [2, 3, 4], atol=1e-8),
((1.0, b.reshape(2, 2, 2)),),
((1.0, b.reshape((2, 2, 2))),),
)
assert mixtures_equal(
cirq.partial_trace_of_state_vector_as_mixture(state, [5, 6, 7, 8], atol=1e-8),
((1.0, c.reshape(2, 2, 2, 2)),),
((1.0, c.reshape((2, 2, 2, 2))),),
)
assert mixtures_equal(
cirq.partial_trace_of_state_vector_as_mixture(state, [0, 1, 2, 3, 4], atol=1e-8),
Expand Down
20 changes: 10 additions & 10 deletions cirq-core/cirq/ops/linear_combinations_test.py
Expand Up @@ -1318,7 +1318,7 @@ def test_expectation_from_state_vector_two_qubit_states():
psum1 = cirq.Z(q[0]) + 3.2 * cirq.Z(q[1])
psum2 = -1 * cirq.X(q[0]) + 2 * cirq.X(q[1])
wf1 = np.array([0, 1, 0, 0], dtype=complex)
for state in [wf1, wf1.reshape(2, 2)]:
for state in [wf1, wf1.reshape((2, 2))]:
np.testing.assert_allclose(
psum1.expectation_from_state_vector(state, qubit_map=q_map), -2.2, atol=1e-7
)
Expand All @@ -1327,7 +1327,7 @@ def test_expectation_from_state_vector_two_qubit_states():
)

wf2 = np.array([1, 1, 1, 1], dtype=complex) / 2
for state in [wf2, wf2.reshape(2, 2)]:
for state in [wf2, wf2.reshape((2, 2))]:
np.testing.assert_allclose(
psum1.expectation_from_state_vector(state, qubit_map=q_map), 0, atol=1e-7
)
Expand All @@ -1338,7 +1338,7 @@ def test_expectation_from_state_vector_two_qubit_states():
psum3 = cirq.Z(q[0]) + cirq.X(q[1])
wf3 = np.array([1, 1, 0, 0], dtype=complex) / np.sqrt(2)
q_map_2 = {q0: 1, q1: 0}
for state in [wf3, wf3.reshape(2, 2)]:
for state in [wf3, wf3.reshape((2, 2))]:
np.testing.assert_allclose(
psum3.expectation_from_state_vector(state, qubit_map=q_map), 2, atol=1e-7
)
Expand All @@ -1352,7 +1352,7 @@ def test_expectation_from_density_matrix_invalid_input():
psum = cirq.X(q0) + 2 * cirq.Y(q1) + 3 * cirq.Z(q3)
q_map = {q0: 0, q1: 1, q3: 2}
wf = np.array([1, 0, 0, 0, 0, 0, 0, 0], dtype=np.complex64)
rho = np.kron(wf.conjugate().T, wf).reshape(8, 8)
rho = np.kron(wf.conjugate().T, wf).reshape((8, 8))

im_psum = (1j + 1) * psum
with pytest.raises(NotImplementedError, match='non-Hermitian'):
Expand Down Expand Up @@ -1459,24 +1459,24 @@ def test_expectation_from_density_matrix_two_qubit_states():
psum1 = cirq.Z(q[0]) + 3.2 * cirq.Z(q[1])
psum2 = -1 * cirq.X(q[0]) + 2 * cirq.X(q[1])
wf1 = np.array([0, 1, 0, 0], dtype=complex)
rho1 = np.kron(wf1, wf1).reshape(4, 4)
for state in [rho1, rho1.reshape(2, 2, 2, 2)]:
rho1 = np.kron(wf1, wf1).reshape((4, 4))
for state in [rho1, rho1.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(
psum1.expectation_from_density_matrix(state, qubit_map=q_map), -2.2
)
np.testing.assert_allclose(psum2.expectation_from_density_matrix(state, qubit_map=q_map), 0)

wf2 = np.array([1, 1, 1, 1], dtype=complex) / 2
rho2 = np.kron(wf2, wf2).reshape(4, 4)
for state in [rho2, rho2.reshape(2, 2, 2, 2)]:
rho2 = np.kron(wf2, wf2).reshape((4, 4))
for state in [rho2, rho2.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(psum1.expectation_from_density_matrix(state, qubit_map=q_map), 0)
np.testing.assert_allclose(psum2.expectation_from_density_matrix(state, qubit_map=q_map), 1)

psum3 = cirq.Z(q[0]) + cirq.X(q[1])
wf3 = np.array([1, 1, 0, 0], dtype=complex) / np.sqrt(2)
rho3 = np.kron(wf3, wf3).reshape(4, 4)
rho3 = np.kron(wf3, wf3).reshape((4, 4))
q_map_2 = {q0: 1, q1: 0}
for state in [rho3, rho3.reshape(2, 2, 2, 2)]:
for state in [rho3, rho3.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(psum3.expectation_from_density_matrix(state, qubit_map=q_map), 2)
np.testing.assert_allclose(
psum3.expectation_from_density_matrix(state, qubit_map=q_map_2), 0
Expand Down
26 changes: 13 additions & 13 deletions cirq-core/cirq/ops/pauli_string_test.py
Expand Up @@ -1030,17 +1030,17 @@ def test_expectation_from_state_vector_entangled_states():
x0x1 = cirq.PauliString(x0x1_pauli_map)
q_map = {q0: 0, q1: 1}
wf1 = np.array([0, 1, 1, 0], dtype=complex) / np.sqrt(2)
for state in [wf1, wf1.reshape(2, 2)]:
for state in [wf1, wf1.reshape((2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), -1)
np.testing.assert_allclose(x0x1.expectation_from_state_vector(state, q_map), 1)

wf2 = np.array([1, 0, 0, 1], dtype=complex) / np.sqrt(2)
for state in [wf2, wf2.reshape(2, 2)]:
for state in [wf2, wf2.reshape((2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), 1)
np.testing.assert_allclose(x0x1.expectation_from_state_vector(state, q_map), 1)

wf3 = np.array([1, 1, 1, 1], dtype=complex) / 2
for state in [wf3, wf3.reshape(2, 2)]:
for state in [wf3, wf3.reshape((2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_state_vector(state, q_map), 0)
np.testing.assert_allclose(x0x1.expectation_from_state_vector(state, q_map), 1)

Expand All @@ -1049,7 +1049,7 @@ def test_expectation_from_state_vector_qubit_map():
q0, q1, q2 = _make_qubits(3)
z = cirq.PauliString({q0: cirq.Z})
wf = np.array([0, 1, 0, 1, 0, 0, 0, 0], dtype=complex) / np.sqrt(2)
for state in [wf, wf.reshape(2, 2, 2)]:
for state in [wf, wf.reshape((2, 2, 2))]:
np.testing.assert_allclose(
z.expectation_from_state_vector(state, {q0: 0, q1: 1, q2: 2}), 1, atol=1e-8
)
Expand Down Expand Up @@ -1124,7 +1124,7 @@ def test_expectation_from_density_matrix_invalid_input():
q0, q1, q2, q3 = _make_qubits(4)
ps = cirq.PauliString({q0: cirq.X, q1: cirq.Y})
wf = cirq.testing.random_superposition(4)
rho = np.kron(wf.conjugate().T, wf).reshape(4, 4)
rho = np.kron(wf.conjugate().T, wf).reshape((4, 4))
q_map = {q0: 0, q1: 1}

im_ps = (1j + 1) * ps
Expand Down Expand Up @@ -1238,20 +1238,20 @@ def test_expectation_from_density_matrix_entangled_states():
q_map = {q0: 0, q1: 1}

wf1 = np.array([0, 1, 1, 0], dtype=complex) / np.sqrt(2)
rho1 = np.kron(wf1, wf1).reshape(4, 4)
for state in [rho1, rho1.reshape(2, 2, 2, 2)]:
rho1 = np.kron(wf1, wf1).reshape((4, 4))
for state in [rho1, rho1.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_density_matrix(state, q_map), -1)
np.testing.assert_allclose(x0x1.expectation_from_density_matrix(state, q_map), 1)

wf2 = np.array([1, 0, 0, 1], dtype=complex) / np.sqrt(2)
rho2 = np.kron(wf2, wf2).reshape(4, 4)
for state in [rho2, rho2.reshape(2, 2, 2, 2)]:
rho2 = np.kron(wf2, wf2).reshape((4, 4))
for state in [rho2, rho2.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_density_matrix(state, q_map), 1)
np.testing.assert_allclose(x0x1.expectation_from_density_matrix(state, q_map), 1)

wf3 = np.array([1, 1, 1, 1], dtype=complex) / 2
rho3 = np.kron(wf3, wf3).reshape(4, 4)
for state in [rho3, rho3.reshape(2, 2, 2, 2)]:
rho3 = np.kron(wf3, wf3).reshape((4, 4))
for state in [rho3, rho3.reshape((2, 2, 2, 2))]:
np.testing.assert_allclose(z0z1.expectation_from_density_matrix(state, q_map), 0)
np.testing.assert_allclose(x0x1.expectation_from_density_matrix(state, q_map), 1)

Expand All @@ -1260,9 +1260,9 @@ def test_expectation_from_density_matrix_qubit_map():
q0, q1, q2 = _make_qubits(3)
z = cirq.PauliString({q0: cirq.Z})
wf = np.array([0, 1, 0, 1, 0, 0, 0, 0], dtype=complex) / np.sqrt(2)
rho = np.kron(wf, wf).reshape(8, 8)
rho = np.kron(wf, wf).reshape((8, 8))

for state in [rho, rho.reshape(2, 2, 2, 2, 2, 2)]:
for state in [rho, rho.reshape((2, 2, 2, 2, 2, 2))]:
np.testing.assert_allclose(
z.expectation_from_density_matrix(state, {q0: 0, q1: 1, q2: 2}), 1
)
Expand Down
4 changes: 0 additions & 4 deletions dev_tools/requirements/deps/pylint.txt
@@ -1,6 +1,2 @@
# for linting

pylint~=2.13.0

# Bug with dill 0.3.5, see #5383
dill==0.3.4
4 changes: 4 additions & 0 deletions dev_tools/requirements/pylint.env.txt
@@ -0,0 +1,4 @@
# let pylint check arguments in dependency function calls
-r deps/cirq-all.txt

-r deps/pylint.txt

0 comments on commit 6565fc5

Please sign in to comment.