Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_boson_operator(operator, hbar=1.):
"""Convert to BosonOperator.
Args:
operator: QuadOperator.
hbar (float): the value of hbar used in the definition
of the commutator [q_i, p_j] = i hbar delta_ij.
By default hbar=1.
Returns:
boson_operator: An instance of the BosonOperator class.
"""
boson_operator = BosonOperator()
if isinstance(operator, QuadOperator):
for term, coefficient in operator.terms.items():
tmp = BosonOperator('', coefficient)
for i, d in term:
if d == 'q':
coeff = numpy.sqrt(hbar/2)
sign = 1
elif d == 'p':
coeff = -1j*numpy.sqrt(hbar/2)
sign = -1
tmp *= coeff*(BosonOperator(((i, 0)))
+ BosonOperator(((i, 1)), sign))
boson_operator += tmp
"""
boson_operator = BosonOperator()
if isinstance(operator, QuadOperator):
for term, coefficient in operator.terms.items():
tmp = BosonOperator('', coefficient)
for i, d in term:
if d == 'q':
coeff = numpy.sqrt(hbar/2)
sign = 1
elif d == 'p':
coeff = -1j*numpy.sqrt(hbar/2)
sign = -1
tmp *= coeff*(BosonOperator(((i, 0)))
+ BosonOperator(((i, 1)), sign))
boson_operator += tmp
else:
raise TypeError("Only QuadOperator is currently "
"supported for get_boson_operator.")
return boson_operator
.. math::
R(\phi) = \exp\left(i \phi \ad \a\right)
=\exp\left(i \frac{\phi}{2} \left(\frac{\x^2+ \p^2}{\hbar}-I\right)\right)
Therefore, :math:`U=e^{-iHt/\hbar}` where :math:`H = -\hbar\ad\a` and :math:`t=\phi`.
Args:
phi (float): the rotation angle
mode (int): the qumode on which the operation acts
hbar (float): the scaling convention chosen in the definition of the quadrature
operators: :math:`[\x,\p]=i\hbar`
Returns:
tuple (BosonOperator, t): tuple containing the Hamiltonian
representing the operation and the propagation time
"""
return -BosonOperator('{}^ {}'.format(mode, mode))*hbar, phi
:math:`H(\phi) = {i}{\hbar}\left(e^{i \phi} \ad_0 \a_1 - e^{-i \phi}\a_0 \ad_1\right)`
and :math:`t=\theta`.
Args:
theta (float): transmitivity angle :math:`\theta` where :math:`t=\cos(\theta)`
phi (float): phase angle :math:`\phi` where :math:`r=e^{i\phi}\sin(\theta)`
mode1 (int): the first qumode :math:`\a_0` on which the operation acts
mode2 (int): the second qumode :math:`\a_1` on which the operation acts
hbar (float): the scaling convention chosen in the definition of the quadrature
operators: :math:`[\x,\p]=i\hbar`
Returns:
tuple (BosonOperator, t): tuple containing the Hamiltonian
representing the operation and the propagation time
"""
H = BosonOperator('{}^ {}'.format(mode1, mode2), np.exp(1j*(np.pi-phi)))
H += BosonOperator('{} {}^'.format(mode1, mode2), -np.exp(-1j*(np.pi-phi)))
return 1j*H*hbar, theta
def hermitian_conjugated(operator):
"""Return Hermitian conjugate of operator."""
# Handle FermionOperator
if isinstance(operator, FermionOperator):
conjugate_operator = FermionOperator()
for term, coefficient in operator.terms.items():
conjugate_term = tuple([(tensor_factor, 1 - action) for
(tensor_factor, action) in reversed(term)])
conjugate_operator.terms[conjugate_term] = coefficient.conjugate()
# Handle BosonOperator
elif isinstance(operator, BosonOperator):
conjugate_operator = BosonOperator()
for term, coefficient in operator.terms.items():
conjugate_term = tuple([(tensor_factor, 1 - action) for
(tensor_factor, action) in reversed(term)])
# take into account that different indices commute
conjugate_term = tuple(
sorted(conjugate_term, key=lambda factor: factor[0]))
conjugate_operator.terms[conjugate_term] = coefficient.conjugate()
# Handle QubitOperator
elif isinstance(operator, QubitOperator):
conjugate_operator = QubitOperator()
for term, coefficient in operator.terms.items():
conjugate_operator.terms[term] = coefficient.conjugate()
# Handle QuadOperator
elif isinstance(operator, QuadOperator):
elif operator_type == 'QuadOperator':
operator = QuadOperator(operator_terms)
else:
raise TypeError('Operator of invalid type.')
else:
with open(file_path, 'rb') as f:
data = marshal.load(f)
operator_type = data[0]
operator_terms = data[1]
if operator_type == 'FermionOperator':
operator = FermionOperator()
for term in operator_terms:
operator += FermionOperator(term, operator_terms[term])
elif operator_type == 'BosonOperator':
operator = BosonOperator()
for term in operator_terms:
operator += BosonOperator(term, operator_terms[term])
elif operator_type == 'QubitOperator':
operator = QubitOperator()
for term in operator_terms:
operator += QubitOperator(term, operator_terms[term])
elif operator_type == 'QuadOperator':
operator = QuadOperator()
for term in operator_terms:
operator += QuadOperator(term, operator_terms[term])
else:
raise TypeError('Operator of invalid type.')
return operator
If set to False, then instead the coefficients are taken into
account; S(q^m p^n) = a S(q^m p^n). In this case, if
a is a complex coefficient, it is not guaranteed that the
the returned operator will be Hermitian.
ignore_identity (bool): By default, identity terms are ignore;
S(I) = 0. If set to False, then instead S(I) = I.
Returns:
transformed_operator: an operator of the same class as in the input.
Warning:
The runtime of this method is exponential in the maximum locality
of the original operator.
"""
if isinstance(operator, BosonOperator):
transformed_operator = BosonOperator()
for term in operator.terms:
if ignore_coeff:
coeff = 1
else:
coeff = operator.terms[term]
# Initialize identity matrix.
transformed_term = BosonOperator('', coeff)
if term:
# convert term into the form \prod_i {bd_i^m b_i^n}
modes = dict()
for op in term:
if op[0] not in modes:
modes[op[0]] = [0, 0]
Therefore, :math:`U=e^{-iHt/\hbar}` where
:math:`H(\phi) = {i}{\hbar}\left(e^{i \phi} \ad_0 \a_1 - e^{-i \phi}\a_0 \ad_1\right)`
and :math:`t=\theta`.
Args:
theta (float): transmitivity angle :math:`\theta` where :math:`t=\cos(\theta)`
phi (float): phase angle :math:`\phi` where :math:`r=e^{i\phi}\sin(\theta)`
mode1 (int): the first qumode :math:`\a_0` on which the operation acts
mode2 (int): the second qumode :math:`\a_1` on which the operation acts
hbar (float): the scaling convention chosen in the definition of the quadrature
operators: :math:`[\x,\p]=i\hbar`
Returns:
tuple (BosonOperator, t): tuple containing the Hamiltonian
representing the operation and the propagation time
"""
H = BosonOperator('{}^ {}'.format(mode1, mode2), np.exp(1j*(np.pi-phi)))
H += BosonOperator('{} {}^'.format(mode1, mode2), -np.exp(-1j*(np.pi-phi)))
return 1j*H*hbar, theta
Args:
operator: an instance of the FermionOperator, BosonOperator,
QuadOperator, or InteractionOperator classes.
hbar (float): the value of hbar used in the definition of the
commutator [q_i, p_j] = i hbar delta_ij. By default hbar=1.
This argument only applies when normal ordering QuadOperators.
"""
kwargs = {}
if isinstance(operator, FermionOperator):
ordered_operator = FermionOperator()
order_fn = normal_ordered_ladder_term
kwargs['parity'] = -1
elif isinstance(operator, BosonOperator):
ordered_operator = BosonOperator()
order_fn = normal_ordered_ladder_term
kwargs['parity'] = 1
elif isinstance(operator, QuadOperator):
ordered_operator = QuadOperator()
order_fn = normal_ordered_quad_term
kwargs['hbar'] = hbar
elif isinstance(operator, InteractionOperator):
constant = operator.constant
n_modes = operator.n_qubits
one_body_tensor = operator.one_body_tensor.copy()
two_body_tensor = numpy.zeros_like(operator.two_body_tensor)
quadratic_index_pairs = (
(pq, pq) for pq in itertools.combinations(range(n_modes)[::-1], 2))
def hermitian_conjugated(operator):
"""Return Hermitian conjugate of operator."""
# Handle FermionOperator
if isinstance(operator, FermionOperator):
conjugate_operator = FermionOperator()
for term, coefficient in operator.terms.items():
conjugate_term = tuple([(tensor_factor, 1 - action) for
(tensor_factor, action) in reversed(term)])
conjugate_operator.terms[conjugate_term] = coefficient.conjugate()
# Handle BosonOperator
elif isinstance(operator, BosonOperator):
conjugate_operator = BosonOperator()
for term, coefficient in operator.terms.items():
conjugate_term = tuple([(tensor_factor, 1 - action) for
(tensor_factor, action) in reversed(term)])
# take into account that different indices commute
conjugate_term = tuple(
sorted(conjugate_term, key=lambda factor: factor[0]))
conjugate_operator.terms[conjugate_term] = coefficient.conjugate()
# Handle QubitOperator
elif isinstance(operator, QubitOperator):
conjugate_operator = QubitOperator()
for term, coefficient in operator.terms.items():
conjugate_operator.terms[term] = coefficient.conjugate()
# Handle QuadOperator