quantum-circuit icon indicating copy to clipboard operation
quantum-circuit copied to clipboard

Incorrect Partitioning

Open glanzz opened this issue 1 year ago • 2 comments

I am trying to run the following circuit with and without partitioning:

import QuantumCircuit from 'quantum-circuit';
import fs from 'fs';


function getGates(circuit) {
  var gates  = {}
  circuit.forEach(qubit => {
    qubit.forEach(gate => {
      if (gate) {
        gates[gate.name] = gates[gate.name] ? gates[gate.name] + 1 : 1;
      }
    })
  });
  return gates;
}
function loadQASMCircuit(file) {
  let data = fs.readFileSync(file, 'utf-8');
  if (data) {
    const circuit = new QuantumCircuit();
    circuit.importQASM(data, function(errors) {
      if (errors) {
        console.log("Errors:",errors);
      }
    });
    return circuit;
  } else {
    console.error('Error reading file:', err);
    return null;
  }
}

const neewss = loadQASMCircuit("mycircuit.qasm")
/*
// mycircuit.qasm
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[2];
h q[0];
h q[1];
h q[2];
s q[2];
s q[2];
h q[2];
h q[2];
cx q[0],q[2];
cx q[1],q[2];
h q[0];
h q[1];
h q[1];
h q[0];
cx q[1],q[2];
cx q[0],q[2];
h q[2];
x q[2];
h q[1];
h q[0];
*/


for(let circuit of [neewss]) {
  const shots = 100;
  const results = {};
  console.log("********".repeat(10))
  let initalValues = []
  for(let i =0; i<circuit.numQubits; i++) {
    initalValues.push(1);
  }

  console.log(initalValues)
  for (let i = 0; i < shots; i++) {
      circuit.run(initalValues, {partitioning: false});
      const measuredState = circuit.measureAll();
      const bitstring = measuredState.map(bit => bit ? '1' : '0').join('');
      if (!results[bitstring]) {
          results[bitstring] = 0;
      }
      results[bitstring]++;
  }
  console.log(getGates(circuit.gates))
  console.log(results)
}

The expected result is state 111 for all 100 shots i.e: { '111': 100 } But with partitioning the result is not as expected it is {'010': 100}

What is partitioning meant to do, i see that documentation lacks this...

glanzz avatar Aug 07 '24 22:08 glanzz

@glanzz partitioning is meant to be used to split circuit into smaller "partitions" when circuit is separable. For example, if 30-qubit circuit doesn't have any connection between first 15 qubits and last 15 qubits (there is no multi-qubit gates between any of first 15 qubits and any of second 15 qubits), then circuit is separable into two 15-qubit circuits. That is two "partitions" which can be executed separately and their results combined into one 30-qubit state vector.

However, this idea is abandoned early, and partitioning code is not maintained, but is still present in the source code.

I advise you: do not use it.

perak avatar Aug 07 '24 23:08 perak

How about other options provided by the compiler ?

glanzz avatar Aug 09 '24 17:08 glanzz