【问题标题】:Multiclass classification using TensorFlow Quantum使用 TensorFlow Quantum 进行多类分类
【发布时间】:2020-12-12 09:49:32
【问题描述】:

我正在 TensorFlow Quantum (TFQ) 上运行一些示例和测试,并且正在努力执行多类分类。我将使用 MNIST 分类示例作为基础 (https://www.tensorflow.org/quantum/tutorials/mnist),因为这也是我的起点。

对于二元分类,我使用了不同的类示例和不同的门组合,分类结果是通过测量单个读出的 qubit (qR) 结果获得的,因此如果 qR=0,我们将分类为 0 类,如果 qR= 1 然后我们有第 1 课。

我将它扩展到多类问题,所以我们有 4 个类 (0,1,2,3)。为此,我使用tf.keras.utils.to_categorical(y_train) 更改类的标签,以便标签从单个值转换为向量 (0 -> (1,0,0,0); 1-> (0,1,0, 0); etc..),使用tf.keras.losses.CategoricalHinge() 作为模型的损失并创建 4 个读数量子位,每个类一个 (M(qR0, qR1, qR2, qR3) = (0,0,1,0) -> 2 类),这行得通。

但是,这种方法会大大增加电路的尺寸。所以我想要做的是只将 2 个读出 qubits 传递给 TFQ,并使用 4 类分类的组合测量值(|00> = 0,|10> = 1,|01> = 2,|11> = 3) .理想情况下,这将允许 2^n 多类分类,其中 n 是量子比特的数量。在 Cirq 中,我可以通过在两个读出量子位上执行 cirq.measure(qR0, qR1, key='measure') 来实现此输出。但是,我在将此类命令传递给 TFQ 时遇到了困难,因为据我了解,它仅测量以单个量子比特 Pauli 门结尾的量子比特。

那么,我在 TFQ 的功能中是否缺少允许在训练过程中进行此类测量的功能?

【问题讨论】:

    标签: tensorflow multiclass-classification quantum-computing tensorflow-quantum cirq


    【解决方案1】:

    从这个sn-p开始:

    bit = cirq.GridQubit(0, 0)
    symbols = sympy.symbols('x, y, z')
    
    # !This is important!
    ops = [-1.0 * cirq.Z(bit), cirq.X(bit) + 2.0 * cirq.Z(bit)]
    # !This is important!
    
    circuit_list = [
        _gen_single_bit_rotation_problem(bit, symbols),
        cirq.Circuit(
            cirq.Z(bit) ** symbols[0],
            cirq.X(bit) ** symbols[1],
            cirq.Z(bit) ** symbols[2]
        ),
        cirq.Circuit(
            cirq.X(bit) ** symbols[0],
            cirq.Z(bit) ** symbols[1],
            cirq.X(bit) ** symbols[2]
        )
    ]
    expectation_layer = tfq.layers.Expectation()
    output = expectation_layer(
        circuit_list, symbol_names=symbols, operators = ops)
    # Here output[i][j] corresponds to the expectation of all the ops
    # in ops w.r.t circuits[i] where keras managed variables are
    # placed in the symbols 'x', 'y', 'z'.
    tf.shape(output)
    
    

    我从这里拿的:https://www.tensorflow.org/quantum/api_docs/python/tfq/layers/Expectation

    output 张量的形状是[3, 2],其中我有 3 个不同的电路,我在每个电路上取了两个期望值。 output[1, 0] 的值将是:

    那么output[2, 1] 的值将是:

    output 的值的形状和内容部分取决于ops 的形状和内容。如果我想制作输出形状[3, 3],我可以将另一个有效的cirq.PauliSum 对象添加到ops 列表中。在您的情况下,如果您希望在两个特定的 cirq.GridQubits q0q1 上获得 00、01、10、11 的概率,您可以执行以下操作:

    def zero_proj(qubit):
      return (1 + cirq.Z(qubit)) / 2
    
    def one_proj(qubit):
      return (1 - cirq.Z(qubit)) / 2
    
    # ! This is important
    ops = [
      zero_proj(q0) * zero_proj(q1),
      zero_proj(q0) * one_proj(q1),
      one_proj(q0) * zero_proj(q1),
      one_proj(q0)* one_proj(q1)
    ]
    # ! This is important
    
    

    制作摄取ops的任何层的输出形状:[whatever_your_batch_size_is, 4]。这是否有助于解决问题?

    【讨论】:

    • 谢谢您的回答!是的,它确实有效,它确实消除了我对 TensorFlow Quantum 的一些不正确理解。
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    相关资源
    最近更新 更多