def test_bit_link_output(self):
        """Test link output."""
        bit_event = events.BitEvents(self.qubits[0], self.instructions, 250)

        links = list(bit_event.get_gate_links())
        ref_list = [
            types.GateLink(t0=35.0, opname=library.CXGate().name,
                           bits=[self.qubits[0], self.qubits[1]]),
            types.GateLink(t0=250.0, opname=library.Measure().name,
                           bits=[self.qubits[0], self.clbits[0]])
        ]

        self.assertListEqual(links, ref_list)
    def setUp(self) -> None:
        """Setup."""
        super().setUp()

        self.qubits = list(qiskit.QuantumRegister(2))
        self.gate_link = types.GateLink(t0=100, opname="cx", bits=self.qubits)

        style = stylesheet.QiskitTimelineStyle()
        self.formatter = style.formatter
 def get_gate_links(self) -> Iterator[types.GateLink]:
     """Return link between multi-bit gates."""
     for inst in self.get_gates():
         # generate link iff this is the primary bit.
         if len(inst.bits) > 1 and inst.bit_position == 0:
             t0 = inst.t0 + 0.5 * inst.duration
             link = types.GateLink(t0=t0,
                                   opname=inst.operand.name,
                                   bits=inst.bits)
             yield link
示例#4
0
    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)