We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Support multi-qubit measurements in `cirq.plot_state_histogram`
Quote from the docstring: "Currently this function assumes each measurement gate applies to only a single qubit."

Currently, I get an incorrect histogram if I didn't read the docstring and used a multi-qubit measurement (I always make circuits like this):
```python
qubits = cirq.LineQubit.range(3)
c = cirq.Circuit(
    (cirq.X**0.4).on_each(*qubits),
    cirq.measure(*qubits),  # One multi-qubit measurement
)
cirq.plot_state_histogram(cirq.sample(c, repetitions=10000))
# Incorrect output, no warning or error
```
![index2](https://user-images.githubusercontent.com/2476062/82605089-e5c42400-9ba4-11ea-8eb5-516586620a45.png)

If I use single-qubit measurement gates, I get the expected histogram:
```python
qubits = cirq.LineQubit.range(3)
c = cirq.Circuit(
    (cirq.X**0.4).on_each(*qubits),
    cirq.measure_each(*qubits),  # One measurement per qubit
)
cirq.plot_state_histogram(cirq.sample(c, repetitions=10000))
```
![index](https://user-images.githubusercontent.com/2476062/82605108-ebba0500-9ba4-11ea-9752-f337ecab1d26.png)

This looks like it could be fixed by adding some logic to `plot_state_histogram` (https://github.com/quantumlib/Cirq/blob/master/cirq/study/visualize.py#L22) that checks for multi-qubit measurements and either correctly interpret them or raise an error.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `cirq/study/visualize.py`
Content:
```
1 # Copyright 2018 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 """Tool to visualize the results of a study."""
16 
17 import numpy as np
18 
19 from cirq.study import trial_result
20 
21 
22 def plot_state_histogram(result: trial_result.TrialResult) -> np.ndarray:
23     """Plot the state histogram from a single result with repetitions.
24 
25     States is a bitstring representation of all the qubit states in a single
26     result.
27     Currently this function assumes each measurement gate applies to only
28     a single qubit.
29 
30     Args:
31         result: The trial results to plot.
32 
33     Returns:
34         The histogram. A list of values plotted on the y-axis.
35     """
36 
37     # pyplot import is deferred because it requires a system dependency
38     # (python3-tk) that `python -m pip install cirq` can't handle for the user.
39     # This allows cirq to be usable without python3-tk.
40     import matplotlib.pyplot as plt
41 
42     num_qubits = len(result.measurements.keys())
43     states = 2**num_qubits
44     values = np.zeros(states)
45 
46     # measurements is a dict of {measurement gate key:
47     #                            array(repetitions, boolean result)}
48     # Convert this to an array of repetitions, each with an array of booleans.
49     # e.g. {q1: array([[True, True]]), q2: array([[False, False]])}
50     #      --> array([[True, False], [True, False]])
51     measurement_by_result = np.array([
52         v.transpose()[0] for k, v in result.measurements.items()]).transpose()
53 
54     for meas in measurement_by_result:
55         # Convert each array of booleans to a string representation.
56         # e.g. [True, False] -> [1, 0] -> '10' -> 2
57         state_ind = int(''.join([str(x) for x in [int(x) for x in meas]]), 2)
58         values[state_ind] += 1
59 
60     plot_labels = [bin(x)[2:].zfill(num_qubits) for x in range(states)]
61     plt.bar(np.arange(states), values, tick_label=plot_labels)
62     plt.xlabel('qubit state')
63     plt.ylabel('result count')
64     plt.show()
65 
66     return values
67
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/cirq/study/visualize.py b/cirq/study/visualize.py
--- a/cirq/study/visualize.py
+++ b/cirq/study/visualize.py
@@ -24,8 +24,6 @@
 
     States is a bitstring representation of all the qubit states in a single
     result.
-    Currently this function assumes each measurement gate applies to only
-    a single qubit.
 
     Args:
         result: The trial results to plot.
@@ -39,17 +37,15 @@
     # This allows cirq to be usable without python3-tk.
     import matplotlib.pyplot as plt
 
-    num_qubits = len(result.measurements.keys())
+    num_qubits = sum([value.shape[1] for value in result.measurements.values()])
     states = 2**num_qubits
     values = np.zeros(states)
-
     # measurements is a dict of {measurement gate key:
     #                            array(repetitions, boolean result)}
     # Convert this to an array of repetitions, each with an array of booleans.
     # e.g. {q1: array([[True, True]]), q2: array([[False, False]])}
     #      --> array([[True, False], [True, False]])
-    measurement_by_result = np.array([
-        v.transpose()[0] for k, v in result.measurements.items()]).transpose()
+    measurement_by_result = np.hstack(list(result.measurements.values()))
 
     for meas in measurement_by_result:
         # Convert each array of booleans to a string representation.

{"golden_diff": "diff --git a/cirq/study/visualize.py b/cirq/study/visualize.py\n--- a/cirq/study/visualize.py\n+++ b/cirq/study/visualize.py\n@@ -24,8 +24,6 @@\n \n     States is a bitstring representation of all the qubit states in a single\n     result.\n-    Currently this function assumes each measurement gate applies to only\n-    a single qubit.\n \n     Args:\n         result: The trial results to plot.\n@@ -39,17 +37,15 @@\n     # This allows cirq to be usable without python3-tk.\n     import matplotlib.pyplot as plt\n \n-    num_qubits = len(result.measurements.keys())\n+    num_qubits = sum([value.shape[1] for value in result.measurements.values()])\n     states = 2**num_qubits\n     values = np.zeros(states)\n-\n     # measurements is a dict of {measurement gate key:\n     #                            array(repetitions, boolean result)}\n     # Convert this to an array of repetitions, each with an array of booleans.\n     # e.g. {q1: array([[True, True]]), q2: array([[False, False]])}\n     #      --> array([[True, False], [True, False]])\n-    measurement_by_result = np.array([\n-        v.transpose()[0] for k, v in result.measurements.items()]).transpose()\n+    measurement_by_result = np.hstack(list(result.measurements.values()))\n \n     for meas in measurement_by_result:\n         # Convert each array of booleans to a string representation.\n", "issue": "Support multi-qubit measurements in `cirq.plot_state_histogram`\nQuote from the docstring: \"Currently this function assumes each measurement gate applies to only a single qubit.\"\r\n\r\nCurrently, I get an incorrect histogram if I didn't read the docstring and used a multi-qubit measurement (I always make circuits like this):\r\n```python\r\nqubits = cirq.LineQubit.range(3)\r\nc = cirq.Circuit(\r\n    (cirq.X**0.4).on_each(*qubits),\r\n    cirq.measure(*qubits),  # One multi-qubit measurement\r\n)\r\ncirq.plot_state_histogram(cirq.sample(c, repetitions=10000))\r\n# Incorrect output, no warning or error\r\n```\r\n![index2](https://user-images.githubusercontent.com/2476062/82605089-e5c42400-9ba4-11ea-8eb5-516586620a45.png)\r\n\r\nIf I use single-qubit measurement gates, I get the expected histogram:\r\n```python\r\nqubits = cirq.LineQubit.range(3)\r\nc = cirq.Circuit(\r\n    (cirq.X**0.4).on_each(*qubits),\r\n    cirq.measure_each(*qubits),  # One measurement per qubit\r\n)\r\ncirq.plot_state_histogram(cirq.sample(c, repetitions=10000))\r\n```\r\n![index](https://user-images.githubusercontent.com/2476062/82605108-ebba0500-9ba4-11ea-9752-f337ecab1d26.png)\r\n\r\nThis looks like it could be fixed by adding some logic to `plot_state_histogram` (https://github.com/quantumlib/Cirq/blob/master/cirq/study/visualize.py#L22) that checks for multi-qubit measurements and either correctly interpret them or raise an error.\n", "before_files": [{"content": "# Copyright 2018 The Cirq Developers\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Tool to visualize the results of a study.\"\"\"\n\nimport numpy as np\n\nfrom cirq.study import trial_result\n\n\ndef plot_state_histogram(result: trial_result.TrialResult) -> np.ndarray:\n    \"\"\"Plot the state histogram from a single result with repetitions.\n\n    States is a bitstring representation of all the qubit states in a single\n    result.\n    Currently this function assumes each measurement gate applies to only\n    a single qubit.\n\n    Args:\n        result: The trial results to plot.\n\n    Returns:\n        The histogram. A list of values plotted on the y-axis.\n    \"\"\"\n\n    # pyplot import is deferred because it requires a system dependency\n    # (python3-tk) that `python -m pip install cirq` can't handle for the user.\n    # This allows cirq to be usable without python3-tk.\n    import matplotlib.pyplot as plt\n\n    num_qubits = len(result.measurements.keys())\n    states = 2**num_qubits\n    values = np.zeros(states)\n\n    # measurements is a dict of {measurement gate key:\n    #                            array(repetitions, boolean result)}\n    # Convert this to an array of repetitions, each with an array of booleans.\n    # e.g. {q1: array([[True, True]]), q2: array([[False, False]])}\n    #      --> array([[True, False], [True, False]])\n    measurement_by_result = np.array([\n        v.transpose()[0] for k, v in result.measurements.items()]).transpose()\n\n    for meas in measurement_by_result:\n        # Convert each array of booleans to a string representation.\n        # e.g. [True, False] -> [1, 0] -> '10' -> 2\n        state_ind = int(''.join([str(x) for x in [int(x) for x in meas]]), 2)\n        values[state_ind] += 1\n\n    plot_labels = [bin(x)[2:].zfill(num_qubits) for x in range(states)]\n    plt.bar(np.arange(states), values, tick_label=plot_labels)\n    plt.xlabel('qubit state')\n    plt.ylabel('result count')\n    plt.show()\n\n    return values\n", "path": "cirq/study/visualize.py"}], "after_files": [{"content": "# Copyright 2018 The Cirq Developers\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     https://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"Tool to visualize the results of a study.\"\"\"\n\nimport numpy as np\n\nfrom cirq.study import trial_result\n\n\ndef plot_state_histogram(result: trial_result.TrialResult) -> np.ndarray:\n    \"\"\"Plot the state histogram from a single result with repetitions.\n\n    States is a bitstring representation of all the qubit states in a single\n    result.\n\n    Args:\n        result: The trial results to plot.\n\n    Returns:\n        The histogram. A list of values plotted on the y-axis.\n    \"\"\"\n\n    # pyplot import is deferred because it requires a system dependency\n    # (python3-tk) that `python -m pip install cirq` can't handle for the user.\n    # This allows cirq to be usable without python3-tk.\n    import matplotlib.pyplot as plt\n\n    num_qubits = sum([value.shape[1] for value in result.measurements.values()])\n    states = 2**num_qubits\n    values = np.zeros(states)\n    # measurements is a dict of {measurement gate key:\n    #                            array(repetitions, boolean result)}\n    # Convert this to an array of repetitions, each with an array of booleans.\n    # e.g. {q1: array([[True, True]]), q2: array([[False, False]])}\n    #      --> array([[True, False], [True, False]])\n    measurement_by_result = np.hstack(list(result.measurements.values()))\n\n    for meas in measurement_by_result:\n        # Convert each array of booleans to a string representation.\n        # e.g. [True, False] -> [1, 0] -> '10' -> 2\n        state_ind = int(''.join([str(x) for x in [int(x) for x in meas]]), 2)\n        values[state_ind] += 1\n\n    plot_labels = [bin(x)[2:].zfill(num_qubits) for x in range(states)]\n    plt.bar(np.arange(states), values, tick_label=plot_labels)\n    plt.xlabel('qubit state')\n    plt.ylabel('result count')\n    plt.show()\n\n    return values\n", "path": "cirq/study/visualize.py"}]}