【问题标题】:Find the length of the array that was passed to you in Q#查找在 Q# 中传递给您的数组的长度
【发布时间】:2018-12-09 14:45:28
【问题描述】:

我有如下操作,驱动程序需要向其发送一个量子位数组。

operation myOp(qubits: Qubit[]) : () {
     // uses elements from the qubit array        
 }

如何从代码中找到这个数组的长度?

【问题讨论】:

    标签: arrays variable-length-array q# qubit


    【解决方案1】:

    Length(qubits)

    在数值表达式的文档中提到:https://docs.microsoft.com/en-us/quantum/quantum-qr-expressions#numeric-expressions

    【讨论】:

      【解决方案2】:
      let n = Length(qubits)
      

      这会将长度存储在变量 n 中。 n也是一个不能改变的常数。如果出于任何原因你想要一个可变变量 n 然后

      mutable n = Length(qubits) 
      

      可以更改。现在您可以使用 for 循环遍历数组(适用于常量或可变 n)

      for(index in 0 .. (n-1)) {
      //process the element qubits[index]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 2020-08-16
        • 1970-01-01
        • 2020-08-03
        相关资源
        最近更新 更多