def test_barrier_output(self): """Test barrier output.""" bit_event = events.BitEvents(self.qubits[0], self.instructions, 200) barriers = list(bit_event.get_barriers()) ref_list = [ types.Barrier(t0=120, bits=[self.qubits[0], self.qubits[1]], bit_position=0), types.Barrier(t0=200, bits=[self.qubits[0]], bit_position=0) ] self.assertListEqual(barriers, ref_list)
def get_barriers(self) -> Iterator[types.Barrier]: """Return barriers.""" for inst in self.instructions: if isinstance(inst.operand, circuit.Barrier): barrier = types.Barrier(t0=inst.t0, bits=inst.bits, bit_position=inst.bit_position) yield barrier
def setUp(self) -> None: """Setup.""" super().setUp() self.qubits = list(qiskit.QuantumRegister(3)) self.barrier = types.Barrier(t0=100, bits=self.qubits, bit_position=1) style = stylesheet.QiskitTimelineStyle() self.formatter = style.formatter
def load_program(self, program: circuit.QuantumCircuit): """Load quantum circuit and create drawing.. Args: program: Scheduled circuit object to draw. Raises: VisualizationError: When circuit is not scheduled. """ not_gate_like = (circuit.Barrier, ) if getattr(program, "_op_start_times") is None: # Run scheduling for backward compatibility from qiskit import transpile from qiskit.transpiler import InstructionDurations, TranspilerError warnings.warn( "Visualizing un-scheduled circuit with timeline drawer has been deprecated. " "This circuit should be transpiled with scheduler though it consists of " "instructions with explicit durations.", DeprecationWarning, ) try: program = transpile( program, scheduling_method="alap", instruction_durations=InstructionDurations()) except TranspilerError as ex: raise VisualizationError( f"Input circuit {program.name} is not scheduled and it contains " "operations with unknown delays. This cannot be visualized." ) from ex for t0, instruction in zip(program.op_start_times, program.data): bits = list(instruction.qubits) + list(instruction.clbits) for bit_pos, bit in enumerate(bits): if not isinstance(instruction.operation, not_gate_like): # Generate draw object for gates gate_source = types.ScheduledGate( t0=t0, operand=instruction.operation, duration=instruction.operation.duration, bits=bits, bit_position=bit_pos, ) for gen in self.generator["gates"]: obj_generator = partial(gen, formatter=self.formatter) for datum in obj_generator(gate_source): self.add_data(datum) if len(bits) > 1 and bit_pos == 0: # Generate draw object for gate-gate link line_pos = t0 + 0.5 * instruction.operation.duration link_source = types.GateLink( t0=line_pos, opname=instruction.operation.name, bits=bits) for gen in self.generator["gate_links"]: obj_generator = partial(gen, formatter=self.formatter) for datum in obj_generator(link_source): self.add_data(datum) if isinstance(instruction.operation, circuit.Barrier): # Generate draw object for barrier barrier_source = types.Barrier(t0=t0, bits=bits, bit_position=bit_pos) for gen in self.generator["barriers"]: obj_generator = partial(gen, formatter=self.formatter) for datum in obj_generator(barrier_source): self.add_data(datum) self.bits = list(program.qubits) + list(program.clbits) for bit in self.bits: for gen in self.generator["bits"]: # Generate draw objects for bit obj_generator = partial(gen, formatter=self.formatter) for datum in obj_generator(bit): self.add_data(datum) # update time range t_end = max(program.duration, self.formatter["margin.minimum_duration"]) self.set_time_range(t_start=0, t_end=t_end)