0
$\begingroup$

I am continuing my work on the SAT katas, but I thing I am missing something on the way the tuple data type works. In this example code, I am trying to access the data in a tuple type :

let clause = [(0, false), (1, true)];
using (testRegister = qubit[2]) {
    for i in clause {
        if i[1] == false {
            X(testRegister[i[0]]);
        }
    }
}

Executing this code will return the error : error QS5004: The type (Int, Bool) does not provide item access. Items can only be accessed for values of array type. I have trouble understanding this error as it states that the tuple's different values cannot be accessed, or am I not doing it the correct way ? I am definitely not trying to modify these !

$\endgroup$
1
$\begingroup$

The subscripting operator ([]) in Q# only works on values of array types, such as Int[], Qubit[] or Double[][]. To unpack a tuple, you can deconstruct when you assign the tuple in a let statement or for loop:

let clauses = [(0, false), (1, true)];
using (testRegister = qubit[2]) {
    for (idx, value) in clauses {
        if not value {
            X(testRegister[idx]);
        }
    }
}

// Alternatively:
let clauses = [(0, false), (1, true)];
using (testRegister = qubit[2]) {
    for clause in clauses {
        let (idx, value) = clause;
        if not value {
            X(testRegister[idx]);
        }
    }
}

The Q# standard library also provides two convenience functions, Fst and Snd for working with pairs (that is, tuples of two elements):

let clauses = [(0, false), (1, true)];
using (testRegister = qubit[2]) {
    for clause in clauses {
        if Snd(clause) {
            X(testRegister[Fst(idx)]);
        }
    }
}

To see why this is needed, Q# arrays are always homogeneous in type. For example, if arr has type 'T[], then arr[idx] is always of type 'T, as long as idx is within the range of valid indices for arr. By contrast, tuples can be heterogeneous in type; in your example, clauses has type (Int, Bool)[], such that the first and second parts of clauses[0] have different types. If the subscripting operator were supported for tuples, clauses[0][0] would thus have a different type from clauses[0][1]; in particular, the type of an expression like clauses[0][idx] could not be inferred at compile type, since the expression would have a different type depending on whether idx is 0 or 1.

This is a large part of why, to help make quantum programs that work reliably and predictably while allowing the flexiblity and power of tuples, Q# offers destructuring and functions like Fst and Snd to help work with tuples.

$\endgroup$
2
  • $\begingroup$ Thank you for the detailed explanation ! What do you mean by subscripting operator and compile type ? $\endgroup$ – BrockenDuck Feb 12 at 20:26
  • 1
    $\begingroup$ The [] operator is often called the subscripting operator; that is, arr[0] can be thought of as arr subscripted by 0. As for compile-time type, Q# is a statically typed language, such that every variable has a type that is known when you compile the program --- by contrast, in a dynamically typed programming language, the types of variables can change at runtime. There's advantages and disadvantages either way, but static types work great in quantum computing to help make sure your code works reliably. $\endgroup$ – Chris Granade Feb 12 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.