Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Partitioning #93

Open
glanzz opened this issue Aug 7, 2024 · 2 comments
Open

Incorrect Partitioning #93

glanzz opened this issue Aug 7, 2024 · 2 comments

Comments

@glanzz
Copy link

glanzz commented Aug 7, 2024

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...

@perak
Copy link
Collaborator

perak commented Aug 7, 2024

@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.

@glanzz
Copy link
Author

glanzz commented Aug 9, 2024

How about other options provided by the compiler ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants