Repository: Qiskit/qiskit

Issue:
pulse visualization does not truncate a command
Suppose a schedule contains gates from 0 - 500 ns and readout from 500 - 2000 ns. When I use 
```
sched.filter(time_ranges=[(0, 1000)]).draw()
```
I expect the schedule to be drawn up to 1000 ns. This means truncating, but showing, half of the readout. 
However currently it only draws up to 500 ns, because it does not want to truncate the readout. 


Discussion:
please use `plot_range` option.

https://github.com/Qiskit/qiskit-terra/blob/35abeb9da0f8f975931a8f8bd281a64d6df0ba82/qiskit/visualization/pulse/matplotlib.py#L550

if it doesn't draw any acquire or measurement channel, this could be a bug.
Ok thanks the `plot_range` seems to work and it truncates the plot. However in some cases I get this error:
```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-af085778ce60> in <module>()
      1 sched = schedule(new_circ, hed, method='alap')
----> 2 sched.draw(plot_range=(0, 300))

~/Desktop/ajavadia-terra/qiskit/pulse/schedule.py in draw(self, dt, style, filename, interp_method, scaling, channels_to_plot, plot_all, plot_range, interactive, table, label, framechange)
    327                                           plot_all=plot_all, plot_range=plot_range,
    328                                           interactive=interactive, table=table,
--> 329                                           label=label, framechange=framechange)
    330 
    331     def __eq__(self, other: ScheduleComponent) -> bool:

~/Desktop/ajavadia-terra/qiskit/visualization/pulse_visualization.py in pulse_drawer(data, dt, style, filename, interp_method, scaling, channels_to_plot, plot_all, plot_range, interactive, table, label, framechange)
     60                             plot_range=plot_range, channels_to_plot=channels_to_plot,
     61                             plot_all=plot_all, table=table, label=label,
---> 62                             framechange=framechange)
     63     else:
     64         raise VisualizationError('This data cannot be visualized.')

~/Desktop/ajavadia-terra/qiskit/visualization/pulse/matplotlib.py in draw(self, schedule, dt, interp_method, plot_range, scaling, channels_to_plot, plot_all, table, label, framechange)
    584         n_valid_waveform, v_max = self._count_valid_waveforms(output_channels, scaling=scaling,
    585                                                               channels_to_plot=channels_to_plot,
--> 586                                                               plot_all=plot_all)
    587 
    588         if table:

~/Desktop/ajavadia-terra/qiskit/visualization/pulse/matplotlib.py in _count_valid_waveforms(self, channels, scaling, channels_to_plot, plot_all)
    350                     events.enable = True
    351             else:
--> 352                 if not events.is_empty() or plot_all:
    353                     waveform = events.waveform
    354                     v_max = max(v_max,

~/Desktop/ajavadia-terra/qiskit/visualization/pulse/matplotlib.py in is_empty(self)
    116             bool: if the channel has nothing to plot
    117         """
--> 118         if any(self.waveform) or self.framechanges or self.conditionals or self.snapshots:
    119             return False
    120 

~/Desktop/ajavadia-terra/qiskit/visualization/pulse/matplotlib.py in waveform(self)
     74         """Get waveform."""
     75         if self._waveform is None:
---> 76             self._build_waveform()
     77 
     78         return self._waveform[self.t0:self.tf]

~/Desktop/ajavadia-terra/qiskit/visualization/pulse/matplotlib.py in _build_waveform(self)
    191 
    192                 elif isinstance(command, Acquire):
--> 193                     wf[time:tf] = np.ones(command.duration)
    194                     self._labels[time] = (tf, command)
    195         self._waveform = wf + pv

ValueError: could not broadcast input array from shape (1200) into shape (36)
```

Here's code to reproduce (requires the scheduler branch):
```
from qiskit import *
circ = QuantumCircuit(4, 2)

circ.cx(0, 1)
circ.cx(2, 3)
circ.measure([0,1], [0,1])

IBMQ.load_account()
pok = .... # get poughkeepsie backend

new_circ = transpile(circ, pok)

sched = schedule(new_circ, pok, method='alap')
sched.draw(plot_range=(0, 300))
```

range (0,200) works.
I have also seen that error.
ok, I'll check.

Resolving patch:
diff --git a/qiskit/visualization/pulse/matplotlib.py b/qiskit/visualization/pulse/matplotlib.py
--- a/qiskit/visualization/pulse/matplotlib.py
+++ b/qiskit/visualization/pulse/matplotlib.py
@@ -190,7 +190,7 @@ def _build_waveform(self):
                         last_pv = None
 
                 elif isinstance(command, Acquire):
-                    wf[time:tf] = np.ones(command.duration)
+                    wf[time:tf] = np.ones(tf - time)
                     self._labels[time] = (tf, command)
         self._waveform = wf + pv
 


Test patch:
diff --git a/test/python/visualization/test_pulse_visualization_output.py b/test/python/visualization/test_pulse_visualization_output.py
--- a/test/python/visualization/test_pulse_visualization_output.py
+++ b/test/python/visualization/test_pulse_visualization_output.py
@@ -107,6 +107,19 @@ def test_schedule_matplotlib_drawer(self):
         self.assertImagesAreEqual(filename, self.schedule_matplotlib_reference)
         os.remove(filename)
 
+    # TODO: Enable for refactoring purposes and enable by default when we can
+    # decide if the backend is available or not.
+    @unittest.skipIf(not HAS_MATPLOTLIB, 'matplotlib not available.')
+    @unittest.skip('Useful for refactoring purposes, skipping by default.')
+    def test_truncate_acquisition(self):
+        sched = Schedule()
+        acquire = Acquire(30)
+        sched = sched.insert(0, acquire(self.device.acquires[1],
+                                        self.device.memoryslots[1],
+                                        self.device.registers[1]))
+        # Check ValueError is not thrown
+        sched.draw(plot_range=(0, 15))
+
 
 if __name__ == '__main__':
     unittest.main(verbosity=2)