Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_compose_subsystem(self):
"""Test subsystem compose method."""
# 3-qubit operator
mat = self.rand_matrix(8, 8)
mat_a = self.rand_matrix(2, 2)
mat_b = self.rand_matrix(2, 2)
mat_c = self.rand_matrix(2, 2)
op = Operator(mat)
op1 = Operator(mat_a)
op2 = Operator(np.kron(mat_b, mat_a))
op3 = Operator(np.kron(mat_c, np.kron(mat_b, mat_a)))
# op3 qargs=[0, 1, 2]
targ = np.dot(np.kron(mat_c, np.kron(mat_b, mat_a)), mat)
self.assertEqual(op.compose(op3, qargs=[0, 1, 2]), Operator(targ))
# op3 qargs=[2, 1, 0]
targ = np.dot(np.kron(mat_a, np.kron(mat_b, mat_c)), mat)
self.assertEqual(op.compose(op3, qargs=[2, 1, 0]), Operator(targ))
# op2 qargs=[0, 1]
targ = np.dot(np.kron(np.eye(2), np.kron(mat_b, mat_a)), mat)
self.assertEqual(op.compose(op2, qargs=[0, 1]), Operator(targ))
# op2 qargs=[2, 0]
targ = np.dot(np.kron(mat_a, np.kron(np.eye(2), mat_b)), mat)
def test_multiply(self):
"""Test multiply method."""
mat = self.rand_matrix(4, 4)
val = np.exp(5j)
op = Operator(mat)
self.assertEqual(op.multiply(val), Operator(val * mat))
self.assertEqual(val * op, Operator(val * mat))
def test_tensor(self):
"""Test tensor method."""
mat1 = self.UX
mat2 = np.eye(3, dtype=complex)
mat21 = np.kron(mat2, mat1)
op21 = Operator(mat2).tensor(Operator(mat1))
self.assertEqual(op21.dim, (6, 6))
assert_allclose(op21.data, Operator(mat21).data)
mat12 = np.kron(mat1, mat2)
op12 = Operator(mat1).tensor(Operator(mat2))
self.assertEqual(op12.dim, (6, 6))
assert_allclose(op12.data, Operator(mat12).data)
def _other_to_operator(self, rep, qubits_test_cases, repetitions):
"""Test Other to Operator evolution."""
for nq in qubits_test_cases:
dim = 2**nq
for _ in range(repetitions):
rho = self.rand_rho(dim)
mat = self.rand_matrix(dim, dim)
chan = rep(Operator(mat))
rho1 = DensityMatrix(rho).evolve(chan).data
rho2 = DensityMatrix(rho).evolve(Operator(chan)).data
assert_allclose(rho1, rho2)
def test_dim(self):
"""Test Operator dim property."""
mat = self.rand_matrix(4, 4)
self.assertEqual(Operator(mat).dim, (4, 4))
self.assertEqual(Operator(mat, input_dims=[4], output_dims=[4]).dim, (4, 4))
self.assertEqual(Operator(mat, input_dims=[2, 2], output_dims=[2, 2]).dim, (4, 4))
def test_chi_transpose_random(self):
"""Test transpose of Chi matrices is correct."""
mats = [self.rand_matrix(4, 4) for _ in range(4)]
chans = [Chi(Operator(mat)) for mat in mats]
self._compare_transpose_to_operator(chans, mats)
def test_reshape(self):
"""Test Operator _reshape method."""
op = Operator(self.rand_matrix(8, 8))
self.assertEqual(op.output_dims(), (2, 2, 2))
self.assertEqual(op.input_dims(), (2, 2, 2))
op._reshape(input_dims=[8], output_dims=[8])
self.assertEqual(op.output_dims(), (8,))
self.assertEqual(op.input_dims(), (8,))
op._reshape(input_dims=[4, 2], output_dims=[2, 4])
self.assertEqual(op.output_dims(), (2, 4))
self.assertEqual(op.input_dims(), (4, 2))
qargs (list): a list of Statevector subsystem positions to apply
the operator on.
Returns:
Statevector: the output quantum state.
Raises:
QiskitError: if the operator dimension does not match the
specified Statevector subsystem dimensions.
"""
# Evolution by a circuit or instruction
if isinstance(other, (QuantumCircuit, Instruction)):
return self._evolve_instruction(other, qargs=qargs)
# Evolution by an Operator
if not isinstance(other, Operator):
other = Operator(other)
if qargs is None:
# Evolution on full statevector
if self._dim != other._input_dim:
raise QiskitError(
"Operator input dimension is not equal to statevector dimension."
)
return Statevector(np.dot(other.data, self.data), dims=other.output_dims())
# Otherwise we are applying an operator only to subsystems
# Check dimensions of subsystems match the operator
if self.dims(qargs) != other.input_dims():
raise QiskitError(
"Operator input dimensions are not equal to statevector subsystem dimensions."
)
# Reshape statevector and operator
tensor = np.reshape(self.data, self._shape)
mat = np.reshape(other.data, other._shape)
def add(self, other):
"""Return the operator self + other.
Args:
other (Operator): an operator object.
Returns:
Operator: the operator self + other.
Raises:
QiskitError: if other is not an operator, or has incompatible
dimensions.
"""
if not isinstance(other, Operator):
other = Operator(other)
if self.dim != other.dim:
raise QiskitError("other operator has different dimensions.")
return Operator(self.data + other.data, self.input_dims(),
self.output_dims())
def transpose(self):
"""Return the transpose of the operator."""
return Operator(
np.transpose(self.data), self.input_dims(), self.output_dims())