Quandela / Perceval

An open source framework for programming photonic quantum computers
https://perceval.quandela.net
Other
133 stars 63 forks source link

Conversion of cQASM programs to linear optics circuits #407

Closed burlemarxiste closed 2 weeks ago

burlemarxiste commented 1 month ago

This is my submission for issue #399 .

In addition, since I am now more familiar with the codebase, I propose to do the following within the next few weeks (but outside of the challenge):

  1. Refactor all the converters to use a common dictionary mapping classic gates to circuit elements, without trying to guess them from a unitary:

    • QiskitConverter uses unitaries everywhere.
    • MyQLMConverter recognizes just H and PH and use a unitary matrix elsewhere.
    • CQASMConverter directly uses optical elements.
  2. Add support for arbitrary controlled gates through AXBXC decomposition (2 CNOTs).

burlemarxiste commented 4 weeks ago

The latest commit in the branch has the change you requested: I extract the version number with a regexp and use a different parsing function based on the result.

burlemarxiste commented 3 weeks ago

I somehow missed your concern about the multiple method signatures and the inconsistencies it would introduce with the other converters.

I have fixed it with dynamic dispatch as you suggested.

maxwell04-wq commented 2 weeks ago

Yes, that and some improvement in parsing the cQASM script. I'm working on them and will push my changes in about half a day.

On Mon, Jun 17, 2024, 6:57 PM Eric Bertasi @.***> wrote:

@.**** commented on this pull request.

In perceval/converters/cqasm_converter.py https://github.com/Quandela/Perceval/pull/407#discussion_r1642862679:

  • self,
  • source_file_name: str,
  • use_postselection: bool = False) -> Processor:
  • r"""Convert a cQASM v1 quantum program into a Processor.
  • :param source_file_name: The path to the cQASM program to load
  • :param use_postselection: when True, uses a postprocessed CNOT
  • as the last gate. Otherwise, uses only heralded CNOT
  • :return: the converted processor
  • """
  • lines = open(source_file_name, 'r').readlines()
  • ast = self._v3_ast_from_v1_source(lines)
  • return self.convert(ast, use_postselection)
  • return self.convert_string(
  • open(source_file_name, "r").read(),
  • use_postselection)

Hello @burlemarxiste https://github.com/burlemarxiste and @maxwell04-wq https://github.com/maxwell04-wq ,

We have until Wednesday to merge and close the issue, so that UnitaryHACK takes it into account. From what I understand, we might want a few tests supporting qasm v1 format from @maxwell04-wq https://github.com/maxwell04-wq and I think that's all. Is that right? @maxwell04-wq https://github.com/maxwell04-wq , do you think you can do this before Wednesday?

Thanks, Eric

— Reply to this email directly, view it on GitHub https://github.com/Quandela/Perceval/pull/407#discussion_r1642862679, or unsubscribe https://github.com/notifications/unsubscribe-auth/AODZ2FDRQ2GBRWTCF34TYWLZH3TMTAVCNFSM6AAAAABI2M6EKCVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDCMRSHE4DSNBQGM . You are receiving this because you were mentioned.Message ID: @.***>

maxwell04-wq commented 2 weeks ago

@burlemarxiste I'm having problem with adding the parameter of rotation gates to the list of operands, as the parser fails to create a parametrized circuit. Can you help me debug it?

ericbrts commented 2 weeks ago

Hi,

I think @raksharuia already said controlled rotation gates are out of scope. The task was having the same features as the QiskitConverter, in terms of supported gates too. This means supporting only CX, CZ and swap as 2-qubit gates. So I would say don't bother with CRotation gates ?

maxwell04-wq commented 2 weeks ago

Hi @ericbrts,

The v1 parser supports CX and CZ gates. I'm facing problem with the parametrized gates, as the v3 parser takes in the parameters in the list of operands. I have added the parameter in the list of operands as a cqasm.values.ConstFloat object, but the parser is unable to find the parameter for parametrized gates.

burlemarxiste commented 2 weeks ago

Hello!

@maxwell04-wq , is your problem about creating an AST statement for a parametrized rotation (for converting Rx(0.1) q[0])? If so, look at the following code sample:

    statement = cqasm.semantic.Instruction()
    statement.name = f"b'Rx'"
    index = cqasm.values.IndexRef(
        variable=cqasm.semantic.Variable(name = "b'q'"))
    index.indices = cqasm.values.MultiConstInt()
    index.indices.append(cqasm.values.ConstInt(0))
    statement.operands.append(index)

    angle = cqasm.values.ConstFloat(value=0.1)
    statement.operands.append(angle)

This creates a statement with the name Rx, adds the qubit named q with index 0 to the list of operands, and add a second operand of type ConstFloat with the angle (0.1 here).

Full example:


    cqasm = CQASMConverter.cqasm

    # Create an empty Program object to store the v3 AST
    ast = cqasm.semantic.Program(
        api_version=cqasm.primitives.Version([3]),
        block=cqasm.semantic.Block(
            statements=cqasm.semantic.MultiStatement()),
        variables=cqasm.semantic.MultiVariable()
    )

    # Declare a qubit array of size 1
    typ = cqasm.types.QubitArray(size=1)
    ast.variables.append(
        cqasm.semantic.Variable(name="b'q'", typ=typ))

    # Create a statement, with name Rx
    statement = cqasm.semantic.Instruction()
    statement.name = f"b'Rx'"

    # First operand: reference to qubit q[0]
    index = cqasm.values.IndexRef(
        variable=cqasm.semantic.Variable(name = "b'q'"))
    index.indices = cqasm.values.MultiConstInt()
    index.indices.append(cqasm.values.ConstInt(0))
    statement.operands.append(index)

    # Second operand: angle as a ConstFloat
    angle = cqasm.values.ConstFloat(value=0.1)
    statement.operands.append(angle)

    ast.block.statements.append(statement)

    # Convert this AST to a processor
    pc = CQASMConverter(catalog).convert(ast)
    print(ast)
    pdisplay(pc)

Output:

      ╔[Rx(0.1)]╗
      ║░░░░░░░░░║
(]────╫░░░░░░░░░╫──[)
q[0]  ║░░░░░░░░░║[q[0]]
      ║░░░░░░░░░║
(]────╫░░░░░░░░░╫──[)
q[0]  ║░░░░░░░░░║[q[0]]
      ╚         ╝
.
burlemarxiste commented 2 weeks ago

I have added the parameter in the list of operands as a cqasm.values.ConstFloat

Yes, this is the right approach, maybe the confusing thing is that while the syntax is Rx(0.1) q[0], the angle is to be added as the second operand, not as the first!

maxwell04-wq commented 2 weeks ago

Thanks @burlemarxiste,

It turns out that there was an error in the condition for checking the parameter. It's working now!

maxwell04-wq commented 2 weeks ago

@raksharuia I have modified the v1 parser. Please let me know if any changes are required.

raksharuia commented 2 weeks ago

@raksharuia I have modified the v1 parser. Please let me know if any changes are required.

@maxwell04-wq I do not see your commit on this PR. Last commit is from 4-5 days ago.

maxwell04-wq commented 2 weeks ago

@raksharuia I have modified the v1 parser. Please let me know if any changes are required.

My commits are in the main branch. Edit: I have merged main with cqasm_converter.

raksharuia commented 2 weeks ago

Hello @maxwell04-wq,

You have merged Qandela/main into the cqasm branch and your implementation is in the main of burlemarxiste's fork. Is that the correct final update you want to be merged into this branch?

@burlemarxiste, is it ok if we pick up the commits from your main and include them in the CQASM branch?

maxwell04-wq commented 2 weeks ago

Yes, this is the intended update.

burlemarxiste commented 2 weeks ago

All the work I have done for this issue are in my cqasm_converter branch. I haven't done anything in my main in the past 2 weeks. In any case, feel free to pull whatever is needed for you!

ericbrts commented 2 weeks ago

Yeah, so I cherry-picked changes from your main branch, on which @maxwell04-wq worked, to cqasm_converter. There were conflicts but we've made it work and pass the unit tests with @raksharuia . For next times, when you collaborate with a team, please make sure your work is in sync with the others' work (ultimately being only one git branch).

Anyway, we're happy with the work and will merge it now. Thanks a lot to the both of you.