def test_sample_heavy_set_with_parity():
    """Test that we correctly sample a circuit's heavy set with a parity map"""

    sampler = Mock(spec=cirq.Simulator)
    # Construct a result that returns [1, 0, 1, 0] for the physical qubit
    # measurement, and [0, 1, 1, 0] for the ancilla qubit measurement. The first
    # bitstring "10" is valid and heavy. The second "01" is valid and not
    # heavy. The third and fourth bitstrings "11" and "00" are not valid and
    # dropped.
    result = cirq.ResultDict(
        params=cirq.ParamResolver({}),
        measurements={
            'q(0)': np.array([[1], [0]]),
            'q(1)': np.array([[0], [1]]),
            'q(2)': np.array([[1], [1]]),
            'q(3)': np.array([[0], [0]]),
        },
    )
    sampler.run = MagicMock(return_value=result)
    circuit = cirq.Circuit(cirq.measure(*cirq.LineQubit.range(4)))
    compilation_result = CompilationResult(
        circuit=circuit,
        mapping={q: q for q in cirq.LineQubit.range(4)},
        parity_map={cirq.LineQubit(0): cirq.LineQubit(1), cirq.LineQubit(2): cirq.LineQubit(3)},
    )
    probability = cirq.contrib.quantum_volume.sample_heavy_set(
        compilation_result, [1], sampler=sampler, repetitions=1
    )
    # The first output is in the heavy set. The second one isn't, but it is
    # dropped.
    assert probability == 0.5
Test that we correctly sample a circuit's heavy set with a parity map
cirq-core/cirq/contrib/quantum_volume/quantum_volume_test.py