def test_compare_circuits_superoperator_to_simulation(circuit, initial_state):
    """Compares action of circuit superoperator and circuit simulation."""
    assert circuit._has_superoperator_()
    superoperator = circuit._superoperator_()
    vectorized_initial_state = initial_state.reshape(-1)
    vectorized_final_state = superoperator @ vectorized_initial_state
    actual_state = np.reshape(vectorized_final_state, initial_state.shape)

    sim = cirq.DensityMatrixSimulator()
    expected_state = sim.simulate(circuit, initial_state=initial_state).final_density_matrix

    assert np.allclose(actual_state, expected_state)
Compares action of circuit superoperator and circuit simulation.
cirq-core/cirq/circuits/circuit_test.py