Esempio n. 1
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Expose bindings for tfq utility ops."""
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

UTILITY_OP_MODULE = load_module("_tfq_utility_ops.so")


def append_circuit(programs, programs_to_append):
    """Merge programs in the input tensors.

    Given two tensors of programs, this function merges the programs pairwise
    and returns a single tensor containing the merged programs. Note that this
    function is not differentiable because the output has type string.


    >>> q = cirq.GridQubit(0, 0)
    >>> p0 = [cirq.Circuit(cirq.H(q)), cirq.Circuit(cirq.S(q))]
    >>> p1 = [cirq.Circuit(cirq.Z(q)), cirq.Circuit(cirq.X(q))]
    >>> p0_t = tfq.convert_to_tensor(p0)
    >>> p1_t = tfq.convert_to_tensor(p1)
Esempio n. 2
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module to register python op gradient."""
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

SIM_OP_MODULE = load_module("_tfq_simulate_ops.so")


def tfq_simulate_expectation(programs, symbol_names, symbol_values,
                             pauli_sums):
    """Calculate the expectation value of circuits wrt some operator(s)

    Args:
        programs: `tf.Tensor` of strings with shape [batch_size] containing
            the string representations of the circuits to be executed.
        symbol_names: `tf.Tensor` of strings with shape [n_params], which
            is used to specify the order in which the values in
            `symbol_values` should be placed inside of the circuits in
            `programs`.
        symbol_values: `tf.Tensor` of real numbers with shape
            [batch_size, n_params] specifying parameter values to resolve
Esempio n. 3
0
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module to register python op gradient."""
import os
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

MATH_OP_MODULE = load_module(os.path.join("math_ops", "_tfq_math_ops.so"))


def _inner_product_grad(programs, symbol_names, symbol_values, other_programs,
                        prev_grad):
    """Calculate the adjoint gradients of the inner product between circuits.

    Compute the gradients of the (potentially many) inner products between
    the given circuits and the symbol free comparison circuits.

    Calculates out[i][j][k] = $ \frac{\langle \psi_{\text{programs[i]}} \\
        (\text{symbol_values[i]})}{\partial \text{symbol_names[k]}} | \\
        \psi_{\text{other_programs[j]}} \rangle $


    Note: `other_programs` must not contain any free symbols. These can
Esempio n. 4
0
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module to register python op gradient."""
import tensorflow as tf
from tensorflow_quantum.core.ops import tfq_utility_ops
from tensorflow_quantum.core.ops.load_module import load_module
from tensorflow_quantum.python import quantum_context

OP_MODULE = load_module("_tfq_calculate_unitary_op.so")


def get_unitary_op(
        quantum_concurrent=quantum_context.get_quantum_concurrent_op_mode()):
    """Get an op that calculates the unitary matrix for the given circuits.

    >>> unitary_op = tfq.get_unitary_op()
    >>> qubit = cirq.GridQubit(0, 0)
    >>> symbol = sympy.Symbol('alpha')
    >>> my_circuit = cirq.Circuit(cirq.H(qubit) ** symbol)
    >>> tensor_circuit = tfq.convert_to_tensor([my_circuit])
    >>> unitary_op(tensor_circuit, ['alpha'], [[0.2]])
    <tf.RaggedTensor [
        [[(0.9720+0.0860j), (0.0675-0.2078j)],
         [(0.0675-0.2078j), (0.8369+0.5017j)]]]>
Esempio n. 5
0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module to register python op gradient."""
import tensorflow as tf
from tensorflow_quantum.core.ops.load_module import load_module

SIM_OP_MODULE = load_module("_tfq_adj_grad.so")


def tfq_adj_grad(programs, symbol_names, symbol_values, pauli_sums, prev_grad):
    """Calculate gradient of expectation value of circuits wrt some operator(s).

    Args:
        programs: `tf.Tensor` of strings with shape [batch_size] containing
            the string representations of the circuits to be executed.
        symbol_names: `tf.Tensor` of strings with shape [n_params], which
            is used to specify the order in which the values in
            `symbol_values` should be placed inside of the circuits in
            `programs`.
        symbol_values: `tf.Tensor` of real numbers with shape
            [batch_size, n_params] specifying parameter values to resolve
            into the circuits specificed by programs, following the ordering
Esempio n. 6
0
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Expose bindings for ParameterShift C++ ops."""
from tensorflow_quantum.core.ops.load_module import load_module

PS_UTIL_MODULE = load_module("_tfq_ps_utils.so")

# pylint: disable=invalid-name
tfq_ps_decompose = PS_UTIL_MODULE.tfq_ps_decompose
tfq_ps_symbol_replace = PS_UTIL_MODULE.tfq_ps_symbol_replace
tfq_ps_weights_from_symbols = PS_UTIL_MODULE.tfq_ps_weights_from_symbols
Esempio n. 7
0
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Module for high performance noisy circuit sampling ops"""
import os
import tensorflow as tf
from tensorflow_quantum.core.ops import tfq_utility_ops
from tensorflow_quantum.core.ops.load_module import load_module

NOISY_OP_MODULE = load_module(os.path.join("noise", "_tfq_noise_ops.so"))


def samples(programs, symbol_names, symbol_values, num_samples):
    """Generate samples using the C++ noisy trajectory simulator.

    Simulate the final state of `programs` given `symbol_values` are placed
    inside of the symbols with the name in `symbol_names` in each circuit.
    Channels in this simulation will be "tossed" to a certain realization
    during simulation. After each simulation is a run a single bitstring
    will be drawn. These simulations are repeated `num_samples` times.


    >>> # Sample a noisy circuit with C++.
    >>> qubit = cirq.GridQubit(0, 0)
    >>> my_symbol = sympy.Symbol('alpha')