Example #1
0
    def tensor(self,
               other: OperatorBase) -> Union["CircuitStateFn", TensoredOp]:
        r"""
        Return tensor product between self and other, overloaded by ``^``.
        Note: You must be conscious of Qiskit's big-endian bit printing convention.
        Meaning, Plus.tensor(Zero)
        produces a \|+⟩ on qubit 0 and a \|0⟩ on qubit 1, or \|+⟩⨂\|0⟩, but would produce
        a QuantumCircuit like:

            \|0⟩--
            \|+⟩--

        Because Terra prints circuits and results with qubit 0 at the end of the string or circuit.

        Args:
            other: The ``OperatorBase`` to tensor product with self.

        Returns:
            An ``OperatorBase`` equivalent to the tensor product of self and other.
        """
        if isinstance(other, CircuitStateFn
                      ) and other.is_measurement == self.is_measurement:
            # Avoid reimplementing tensor, just use CircuitOp's
            c_op_self = CircuitOp(self.primitive, self.coeff)
            c_op_other = CircuitOp(other.primitive, other.coeff)
            c_op = c_op_self.tensor(c_op_other)
            if isinstance(c_op, CircuitOp):
                return CircuitStateFn(
                    primitive=c_op.primitive,  # pylint: disable=no-member
                    coeff=c_op.coeff,
                    is_measurement=self.is_measurement)
        return TensoredOp([self, other])
 def tensor(self, other: OperatorBase) -> OperatorBase:
     if isinstance(other, VectorStateFn):
         return StateFn(
             self.primitive.tensor(other.primitive),
             coeff=self.coeff * other.coeff,
             is_measurement=self.is_measurement,
         )
     return TensoredOp([self, other])
    def tensor(self, other: OperatorBase) -> Union["PauliSumOp", TensoredOp]:
        if isinstance(other, PauliSumOp):
            return PauliSumOp(
                self.primitive.tensor(other.primitive),
                coeff=self.coeff * other.coeff,
            )

        return TensoredOp([self, other])
Example #4
0
    def tensor(self, other: OperatorBase) -> OperatorBase:
        # Both Paulis
        if isinstance(other, PauliOp):
            # Copying here because Terra's Pauli kron is in-place.
            op_copy = Pauli((other.primitive.z, other.primitive.x))
            return PauliOp(self.primitive.tensor(op_copy), coeff=self.coeff * other.coeff)

        # pylint: disable=cyclic-import
        from .circuit_op import CircuitOp
        if isinstance(other, CircuitOp):
            return self.to_circuit_op().tensor(other)

        return TensoredOp([self, other])
Example #5
0
    def tensor(self, other: OperatorBase) -> OperatorBase:
        # Both Paulis
        if isinstance(other, PauliOp):
            return PauliOp(self.primitive.tensor(other.primitive), coeff=self.coeff * other.coeff)

        # pylint: disable=cyclic-import
        from .pauli_sum_op import PauliSumOp
        if isinstance(other, PauliSumOp):
            new_primitive = SparsePauliOp(self.primitive).tensor(other.primitive)
            return PauliSumOp(new_primitive, coeff=self.coeff * other.coeff)

        from .circuit_op import CircuitOp
        if isinstance(other, CircuitOp):
            return self.to_circuit_op().tensor(other)

        return TensoredOp([self, other])
Example #6
0
    def tensor(self, other: OperatorBase) -> Union["CircuitOp", TensoredOp]:
        # pylint: disable=cyclic-import
        from .pauli_op import PauliOp
        from .matrix_op import MatrixOp
        if isinstance(other, (PauliOp, CircuitOp, MatrixOp)):
            other = other.to_circuit_op()

        if isinstance(other, CircuitOp):
            new_qc = QuantumCircuit(self.num_qubits + other.num_qubits)
            # NOTE!!! REVERSING QISKIT ENDIANNESS HERE
            new_qc.append(other.to_instruction(),
                          qargs=new_qc.qubits[0:other.primitive.num_qubits])
            new_qc.append(self.to_instruction(),
                          qargs=new_qc.qubits[other.primitive.num_qubits:])
            new_qc = new_qc.decompose()
            return CircuitOp(new_qc, coeff=self.coeff * other.coeff)

        return TensoredOp([self, other])
Example #7
0
    def tensor(self, other: OperatorBase) -> TensoredOp:
        if isinstance(other, TensoredOp):
            return TensoredOp([cast(OperatorBase, self)] + other.oplist)

        return TensoredOp([self, other])
Example #8
0
    def tensor(self, other: OperatorBase) -> Union["MatrixOp", TensoredOp]:
        if isinstance(other, MatrixOp):
            return MatrixOp(self.primitive.tensor(other.primitive),
                            coeff=self.coeff * other.coeff)

        return TensoredOp([self, other])