|
| 1 | +# Copyright 2022 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import cirq |
| 17 | +import pytest |
| 18 | + |
| 19 | +from cirq.experiments.single_qubit_readout_calibration_test import NoisySingleQubitReadoutSampler |
| 20 | + |
| 21 | + |
| 22 | +def get_expected_cm(num_qubits: int, p0: float, p1: float): |
| 23 | + expected_cm = np.zeros((2 ** num_qubits,) * 2) |
| 24 | + for i in range(2 ** num_qubits): |
| 25 | + for j in range(2 ** num_qubits): |
| 26 | + p = 1.0 |
| 27 | + for k in range(num_qubits): |
| 28 | + b0 = (i >> k) & 1 |
| 29 | + b1 = (j >> k) & 1 |
| 30 | + if b0 == 0: |
| 31 | + p *= p0 * b1 + (1 - p0) * (1 - b1) |
| 32 | + else: |
| 33 | + p *= p1 * (1 - b1) + (1 - p1) * b1 |
| 34 | + expected_cm[i][j] = p |
| 35 | + return expected_cm |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize('p0, p1', [(0, 0), (0.2, 0.4), (0.5, 0.5), (0.6, 0.3), (1.0, 1.0)]) |
| 39 | +def test_measure_confusion_matrix_with_noise(p0, p1): |
| 40 | + sampler = NoisySingleQubitReadoutSampler(p0, p1, seed=1234) |
| 41 | + num_qubits = 4 |
| 42 | + qubits = cirq.LineQubit.range(num_qubits) |
| 43 | + expected_cm = get_expected_cm(num_qubits, p0, p1) |
| 44 | + qubits_small = qubits[:2] |
| 45 | + expected_cm_small = get_expected_cm(2, p0, p1) |
| 46 | + repetitions = 12_000 |
| 47 | + # Build entire confusion matrix by running 2 ** 4 = 16 circuits. |
| 48 | + readout_cm = cirq.measure_confusion_matrix(sampler, qubits, repetitions=repetitions) |
| 49 | + assert readout_cm.repetitions == repetitions |
| 50 | + for q, expected in zip([None, qubits_small], [expected_cm, expected_cm_small]): |
| 51 | + np.testing.assert_allclose(readout_cm.confusion_matrix(q), expected, atol=1e-2) |
| 52 | + np.testing.assert_allclose( |
| 53 | + readout_cm.confusion_matrix(q) @ readout_cm.correction_matrix(q), |
| 54 | + np.eye(expected.shape[0]), |
| 55 | + atol=1e-2, |
| 56 | + ) |
| 57 | + |
| 58 | + # Build a tensored confusion matrix using smaller single qubit confusion matrices. |
| 59 | + # This works because the error is uncorrelated and requires only 4 * 2 = 8 circuits. |
| 60 | + readout_cm = cirq.measure_confusion_matrix( |
| 61 | + sampler, [[q] for q in qubits], repetitions=repetitions |
| 62 | + ) |
| 63 | + assert readout_cm.repetitions == repetitions |
| 64 | + for q, expected in zip([None, qubits_small], [expected_cm, expected_cm_small]): |
| 65 | + np.testing.assert_allclose(readout_cm.confusion_matrix(q), expected, atol=1e-2) |
| 66 | + np.testing.assert_allclose( |
| 67 | + readout_cm.confusion_matrix(q) @ readout_cm.correction_matrix(q), |
| 68 | + np.eye(expected.shape[0]), |
| 69 | + atol=1e-2, |
| 70 | + ) |
| 71 | + |
| 72 | + # Apply corrections to sampled probabilities using readout_cm. |
| 73 | + qs = qubits_small |
| 74 | + circuit = cirq.Circuit(cirq.H.on_each(*qs), cirq.measure(*qs)) |
| 75 | + reps = 100_000 |
| 76 | + sampled_result = cirq.get_state_histogram(sampler.run(circuit, repetitions=reps)) / reps |
| 77 | + expected_result = [1 / 4] * 4 |
| 78 | + |
| 79 | + def l2norm(result: np.ndarray): |
| 80 | + return np.sum((expected_result - result) ** 2) |
| 81 | + |
| 82 | + corrected_result = readout_cm.apply(sampled_result, qs) |
| 83 | + assert l2norm(corrected_result) <= l2norm(sampled_result) |
| 84 | + |
| 85 | + |
| 86 | +def test_readout_confusion_matrix_raises(): |
| 87 | + num_qubits = 2 |
| 88 | + confusion_matrix = get_expected_cm(num_qubits, 0.1, 0.2) |
| 89 | + qubits = cirq.LineQubit.range(4) |
| 90 | + with pytest.raises(ValueError, match=r"measure_qubits cannot be empty"): |
| 91 | + _ = cirq.TensoredConfusionMatrices([], [], repetitions=0, timestamp=0) |
| 92 | + |
| 93 | + with pytest.raises(ValueError, match=r"len\(confusion_matrices\)"): |
| 94 | + _ = cirq.TensoredConfusionMatrices( |
| 95 | + [confusion_matrix], [qubits[:2], qubits[2:]], repetitions=0, timestamp=0 |
| 96 | + ) |
| 97 | + |
| 98 | + with pytest.raises(ValueError, match="Shape mismatch for confusion matrix"): |
| 99 | + _ = cirq.TensoredConfusionMatrices(confusion_matrix, qubits, repetitions=0, timestamp=0) |
| 100 | + |
| 101 | + with pytest.raises(ValueError, match="Repeated qubits not allowed"): |
| 102 | + _ = cirq.TensoredConfusionMatrices( |
| 103 | + [confusion_matrix, confusion_matrix], |
| 104 | + [qubits[:2], qubits[1:3]], |
| 105 | + repetitions=0, |
| 106 | + timestamp=0, |
| 107 | + ) |
| 108 | + |
| 109 | + readout_cm = cirq.TensoredConfusionMatrices( |
| 110 | + [confusion_matrix, confusion_matrix], [qubits[:2], qubits[2:]], repetitions=0, timestamp=0 |
| 111 | + ) |
| 112 | + |
| 113 | + with pytest.raises(ValueError, match="should be a subset of"): |
| 114 | + _ = readout_cm.confusion_matrix([cirq.NamedQubit("a")]) |
| 115 | + |
| 116 | + with pytest.raises(ValueError, match="should be a subset of"): |
| 117 | + _ = readout_cm.correction_matrix([cirq.NamedQubit("a")]) |
| 118 | + |
| 119 | + with pytest.raises(ValueError, match="result.shape .* should be"): |
| 120 | + _ = readout_cm.apply(np.asarray([100]), qubits[:2]) |
| 121 | + |
| 122 | + with pytest.raises(ValueError, match="method.* should be"): |
| 123 | + _ = readout_cm.apply(np.asarray([1 / 16] * 16), method='l1norm') |
| 124 | + |
| 125 | + |
| 126 | +def test_readout_confusion_matrix_repr_and_equality(): |
| 127 | + mat1 = cirq.testing.random_orthogonal(4, random_state=1234) |
| 128 | + mat2 = cirq.testing.random_orthogonal(2, random_state=1234) |
| 129 | + q = cirq.LineQubit.range(3) |
| 130 | + a = cirq.TensoredConfusionMatrices([mat1, mat2], [q[:2], q[2:]], repetitions=0, timestamp=0) |
| 131 | + b = cirq.TensoredConfusionMatrices(mat1, q[:2], repetitions=0, timestamp=0) |
| 132 | + c = cirq.TensoredConfusionMatrices(mat2, q[2:], repetitions=0, timestamp=0) |
| 133 | + for x in [a, b, c]: |
| 134 | + cirq.testing.assert_equivalent_repr(x) |
| 135 | + assert cirq.approx_eq(x, x) |
| 136 | + assert x._approx_eq_(mat1, 1e-6) is NotImplemented |
| 137 | + eq = cirq.testing.EqualsTester() |
| 138 | + eq.add_equality_group(a, a) |
| 139 | + eq.add_equality_group(b, b) |
| 140 | + eq.add_equality_group(c, c) |
0 commit comments