Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _compare_multiply_to_superop(self, rep, dim, samples, unitary=False):
"""Test channel scalar multiplication is equivalent to SuperOp"""
for _ in range(samples):
if unitary:
mat1 = self.rand_matrix(dim, dim)
sop1 = np.kron(np.conj(mat1), mat1)
else:
sop1 = self.rand_matrix(dim * dim, dim * dim)
val = 0.7
targ = SuperOp(val * sop1)
channel = SuperOp(rep(SuperOp(sop1)).multiply(val))
self.assertEqual(channel, targ)
def _compare_add_to_superop(self, rep, dim, samples, unitary=False):
"""Test channel addition is equivalent to SuperOp"""
for _ in range(samples):
if unitary:
mat1 = self.rand_matrix(dim, dim)
mat2 = self.rand_matrix(dim, dim)
sop1 = np.kron(np.conj(mat1), mat1)
sop2 = np.kron(np.conj(mat2), mat2)
else:
sop1 = self.rand_matrix(dim * dim, dim * dim)
sop2 = self.rand_matrix(dim * dim, dim * dim)
targ = SuperOp(sop1 + sop2)
channel = SuperOp(rep(SuperOp(sop1)).add(rep(SuperOp(sop2))))
self.assertEqual(channel, targ)
def _compare_subtract_to_superop(self, rep, dim, samples, unitary=False):
"""Test channel subtraction is equivalent to SuperOp"""
for _ in range(samples):
if unitary:
mat1 = self.rand_matrix(dim, dim)
mat2 = self.rand_matrix(dim, dim)
sop1 = np.kron(np.conj(mat1), mat1)
sop2 = np.kron(np.conj(mat2), mat2)
else:
sop1 = self.rand_matrix(dim * dim, dim * dim)
sop2 = self.rand_matrix(dim * dim, dim * dim)
targ = SuperOp(sop1 - sop2)
channel = SuperOp(rep(SuperOp(sop1)).subtract(rep(SuperOp(sop2))))
self.assertEqual(channel, targ)
def test_superop_adjoint(self):
"""Test adjoint of SuperOp matrices is correct."""
mats = self.unitaries
chans = [SuperOp(mat) for mat in self.sops]
self._compare_adjoint_to_operator(chans, mats)
def test_superop_compose(self):
"""Test compose of SuperOp matrices is correct."""
mats = [self.UI, self.UX, self.UY, self.UZ, self.UH]
chans = [
SuperOp(mat)
for mat in [self.sopI, self.sopX, self.sopY, self.sopZ, self.sopH]
]
self._compare_compose_to_operator(chans, mats)
other (QuantumChannel): a quantum channel.
qargs (list): a list of subsystem positions to compose other on.
front (bool): If False compose in standard order other(self(input))
otherwise compose in reverse order self(other(input))
[default: False]
Returns:
SuperOp: The composition channel as a SuperOp object.
Raises:
QiskitError: if other is not a QuantumChannel subclass, or
has incompatible dimensions.
"""
# Convert other to SuperOp
if not isinstance(other, SuperOp):
other = SuperOp(other)
# Check dimensions are compatible
if front and self.input_dims(qargs=qargs) != other.output_dims():
raise QiskitError(
'output_dims of other must match subsystem input_dims')
if not front and self.output_dims(qargs=qargs) != other.input_dims():
raise QiskitError(
'input_dims of other must match subsystem output_dims')
# Full composition of superoperators
if qargs is None:
if front:
# Composition A(B(input))
return SuperOp(np.dot(self._data, other.data),
input_dims=other.input_dims(),
output_dims=self.output_dims())
# Composition B(A(input))
# For superoperator evolution we can simulate a reset as
# a non-unitary superoperator matrix
chan = SuperOp(
np.array([[1, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
[0, 0, 0, 0]]))
if obj.name == 'kraus':
kraus = obj.params
dim = len(kraus[0])
chan = SuperOp(_to_superop('Kraus', (kraus, None), dim, dim))
elif hasattr(obj, 'to_matrix'):
# If instruction is a gate first we see if it has a
# `to_matrix` definition and if so use that.
try:
kraus = [obj.to_matrix()]
dim = len(kraus[0])
chan = SuperOp(_to_superop('Kraus', (kraus, None), dim, dim))
except QiskitError:
pass
return chan
# Convert to dict (for QobjInstruction types)
if hasattr(instr, 'as_dict'):
instr = instr.as_dict()
# Get name and parameters
name = instr.get('name', "")
# Check if reset instruction
if name == 'reset':
# params should be the number of qubits being reset
num_qubits = len(instr['qubits'])
return reset_superop(num_qubits)
# Check if Kraus instruction
if name == 'kraus':
params = instr['params']
return SuperOp(Kraus(params))
return None