Skip to content

Rounding Error in Tensor #137

@alexandru-duca

Description

@alexandru-duca

In order to work towards my original goal, I used Qiskit's mcx to create two different decompositions of a CCCX gate, one without an ancilla called c1 (using the noancilla mode) and one with an ancilla called c2 (using the v-chain mode). For both circuits, qubit 0 is the target qubit, and qubits 1,2 and 3 are the control qubits. Qubit 4 in c2 is the ancilla. I am expecting the following test to work:

/// Theorem 2 from https://arxiv.org/pdf/2208.12820
#[test]
fn multi_controlled_toffoli_with_ancillary_qubits() {
    // Fig. 4a: Multi-controlled Toffoli without ancillary qubits
    let qasm1 = r#"
        [...]
    "#;
    let c1 = Circuit::from_qasm(qasm1).unwrap();

    // Fig. 4b: Multi-controlled Toffoli with ancillary qubits
    let qasm2 = r#"
        [...]
    "#;
    let mut c2 = Circuit::from_qasm(qasm2).unwrap();
    c2.push_front(Gate::new(crate::gate::GType::InitAncilla, vec![4]));
    c2.push_back(Gate::new(crate::gate::GType::PostSelect, vec![4]));
    
    // c1 and c2 are verfiably equal
    assert!(equal_circuit_tensor(&c1, &c2)); // THIS ASSERTION DOES NOT WORK

    // c1 and c2 are not verifiably equal with a simplification-based approach
    // (see Theorem 2 from https://arxiv.org/pdf/2208.12820)
    assert!(equal_circuit_with_options(&c1, &c2, true).is_none()); // THIS ASSERTION WORKS
}

The second assertion works, since the result of the simplification-based equality checker is inconclusive as outlined in Theorem 2. The first assertion does not work however, but it should since both circuits are different decompositions of the same gate.

Both circuits report having the correct number of input qubits (4) and output qubits (4). Here is the tensor of c1 and the tensor of c2. The tensor of c1 contains many values very close to 0 or 1 (presumably because of rounding errors), but which should be precisely either 0 or 1. I was wondering if this can be addressed internally or if the user is supposed to make comparisons up to a certain margin of error.

Note that there currently exists code which compares tensors. Checking for precise equality may need to be changed to something like abs_diff_eq, but unfortunately this method doesn't work for tensors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions