Ejemplo n.º 1
0
def test_add_verbatim_box_with_target(cnot):
    circ = Circuit().add_verbatim_box(cnot, target=[10, 11])
    expected = (Circuit().add_instruction(
        Instruction(compiler_directives.StartVerbatimBox())).add_instruction(
            Instruction(Gate.CNot(), [10, 11])).add_instruction(
                Instruction(compiler_directives.EndVerbatimBox())))
    assert circ == expected
Ejemplo n.º 2
0
def test_add_verbatim_box_no_preceding():
    circ = Circuit().add_verbatim_box(Circuit().h(0)).cnot(2, 3)
    expected = (Circuit().add_instruction(
        Instruction(compiler_directives.StartVerbatimBox())).add_instruction(
            Instruction(Gate.H(), 0)).add_instruction(
                Instruction(
                    compiler_directives.EndVerbatimBox())).add_instruction(
                        Instruction(Gate.CNot(), [2, 3])))
    assert circ == expected
Ejemplo n.º 3
0
def test_add_verbatim_box_different_qubits():
    circ = Circuit().h(1).add_verbatim_box(Circuit().h(0)).cnot(3, 4)
    expected = (Circuit().add_instruction(Instruction(
        Gate.H(),
        1)).add_instruction(Instruction(
            compiler_directives.StartVerbatimBox())).add_instruction(
                Instruction(Gate.H(), 0)).add_instruction(
                    Instruction(
                        compiler_directives.EndVerbatimBox())).add_instruction(
                            Instruction(Gate.CNot(), [3, 4])))
    assert circ == expected
Ejemplo n.º 4
0
def test_adjoint_compiler_directive():
    instr = Instruction(compiler_directives.StartVerbatimBox()).adjoint()
    assert instr == [Instruction(compiler_directives.EndVerbatimBox())]
Ejemplo n.º 5
0
    def add_verbatim_box(
        self,
        verbatim_circuit: Circuit,
        target: QubitSetInput = None,
        target_mapping: Dict[QubitInput, QubitInput] = None,
    ) -> Circuit:
        """
        Add a verbatim `circuit` to self, that is, ensures that `circuit` is not modified in any way
        by the compiler.

        Args:
            verbatim_circuit (Circuit): Circuit to add into self.
            target (int, Qubit, or iterable of int / Qubit, optional): Target qubits for the
                supplied circuit. This is a macro over `target_mapping`; `target` is converted to
                a `target_mapping` by zipping together a sorted `circuit.qubits` and `target`.
                Default = `None`.
            target_mapping (dictionary[int or Qubit, int or Qubit], optional): A dictionary of
                qubit mappings to apply to the qubits of `circuit.instructions`. Key is the qubit
                to map, and the value is what to change it to. Default = `None`.

        Returns:
            Circuit: self

        Raises:
            TypeError: If both `target_mapping` and `target` are supplied.
            ValueError: If `circuit` has result types attached

        Examples:
            >>> widget = Circuit().h(0).h(1)
            >>> circ = Circuit().add_verbatim_box(widget)
            >>> print(list(circ.instructions))
            [Instruction('operator': StartVerbatimBox, 'target': QubitSet([])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(0)])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(1)])),
             Instruction('operator': EndVerbatimBox, 'target': QubitSet([]))]

            >>> widget = Circuit().h(0).cnot(0, 1)
            >>> circ = Circuit().add_verbatim_box(widget, target_mapping={0: 10, 1: 11})
            >>> print(list(circ.instructions))
            [Instruction('operator': StartVerbatimBox, 'target': QubitSet([])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(10)])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(11)])),
             Instruction('operator': EndVerbatimBox, 'target': QubitSet([]))]

            >>> widget = Circuit().h(0).cnot(0, 1)
            >>> circ = Circuit().add_verbatim_box(widget, target=[10, 11])
            >>> print(list(circ.instructions))
            [Instruction('operator': StartVerbatimBox, 'target': QubitSet([])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(10)])),
             Instruction('operator': H('qubit_count': 1), 'target': QubitSet([Qubit(11)])),
             Instruction('operator': EndVerbatimBox, 'target': QubitSet([]))]
        """
        if target_mapping and target is not None:
            raise TypeError("Only one of 'target_mapping' or 'target' can be supplied.")
        elif target is not None:
            keys = sorted(verbatim_circuit.qubits)
            values = target
            target_mapping = dict(zip(keys, values))

        if verbatim_circuit.result_types:
            raise ValueError("Verbatim subcircuit is not measured and cannot have result types")

        if verbatim_circuit.instructions:
            self.add_instruction(Instruction(compiler_directives.StartVerbatimBox()))
            for instruction in verbatim_circuit.instructions:
                self.add_instruction(instruction, target_mapping=target_mapping)
            self.add_instruction(Instruction(compiler_directives.EndVerbatimBox()))
            self._has_compiler_directives = True
        return self