Skip to content

Feat cirq custom gate - #200

Open
MathieuG-Colibri wants to merge 17 commits into
devfrom
feat-cirq-custom-gate
Open

Feat cirq custom gate#200
MathieuG-Colibri wants to merge 17 commits into
devfrom
feat-cirq-custom-gate

Conversation

@MathieuG-Colibri

Copy link
Copy Markdown
Collaborator

Change the cirq custom gate implem and behavior
Cleanup and bug fixing

Refactored to_other_language

@github-actions github-actions Bot added the enhancement New feature or request label May 20, 2026
@MathieuG-Colibri
MathieuG-Colibri marked this pull request as ready for review June 17, 2026 08:25
@MathieuG-Colibri MathieuG-Colibri self-assigned this Jun 17, 2026
Comment thread mpqp/core/instruction/gates/custom_controlled_gate.py Outdated
Comment thread mpqp/tools/cirq.py Outdated
Comment thread mpqp/tools/unitary_decomposition.py Outdated
Comment thread mpqp/tools/unitary_decomposition.py Outdated
Comment thread mpqp/tools/unitary_decomposition.py Outdated
changed = _gray_code(i) ^ _gray_code(i + 1)
control = next(i for i in range(len(thetas)) if (changed >> i & 1))
control = max(-control - position - 1 + circuit.nb_qubits, 1)
control = max(-control - targets[position] - 1 + circuit.nb_qubits, 1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linked to previous comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MathieuG-Colibri can you give an explanation why we needed to introduce targets[position]?

Comment thread mpqp/tools/unitary_decomposition.py Outdated
Comment thread mpqp/tools/unitary_decomposition.py Outdated
Comment thread tests/execution/test_validity.py Outdated
Comment thread tests/execution/test_validity.py
Comment thread mpqp/tools/cirq.py Outdated
Comment thread mpqp/core/circuit.py
Comment thread mpqp/core/circuit.py Outdated
Comment thread mpqp/tools/circuit.py Outdated
Comment thread mpqp/tools/circuit.py Outdated
@MoHermes
MoHermes self-requested a review July 1, 2026 09:24
Comment thread mpqp/tools/circuit.py Outdated
Comment thread mpqp/tools/circuit.py Outdated
Comment thread mpqp/core/instruction/gates/custom_controlled_gate.py Outdated
Comment thread mpqp/core/instruction/gates/custom_gate.py
Comment thread mpqp/core/instruction/gates/custom_gate.py
Comment thread mpqp/tools/maths.py Outdated
targets.sort()

return quantum_shannon_decomposition(
rearrange_matrix(self.matrix, self.targets, do_copy=True), targets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the tests of rearrange_matrix, it looks like they are not really independent from the specific implementation we made, since it reuses it behind the usage of CustomGate, I would add some specific "manual" examples to be 100% sure.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Henri wanted the example to be not circuit related because it's in tools/math.

Nevertheless done 9e33b6c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not reffering to the example in the documentation, I was more talking about tests, in the tests folder. If you look at them, we reuse CustomGate in the test, which makes the comparison in the tests not independant from the feature.

Comment thread mpqp/core/circuit.py Outdated
Comment on lines +614 to +617
if job.circuit.input_g_phase:
import numpy as np

state_vector = state_vector * np.exp(-1j * job.circuit.input_g_phase)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is normally directly done in Result no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the case of cirq (couldn't have known because we didn't implement the cirq gphase before)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But don't we want to do it in the Result class, like we do for other providers, instead of here?
It would be cleaner if we group this kind of correction to the same place no?

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Jul 15, 2026
Comment thread docs/translation.rst Outdated
Comment thread docs/translation.rst
.. automodule:: mpqp.translation.qasm.qasm_to_cirq


Direct translations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be careful when using the term "direct", which is more internal and less standard

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 07d2af5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently not done

Comment thread mpqp/tools/cirq.py Outdated
Comment thread mpqp/tools/maths.py Outdated
G = G0 @ G1.conj().T
g = G0 @ G1.conj().T
G = np.round(g, 10)
eigvals, v = np.linalg.eig(G)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we compute the eigenvalues and vectors on rounded (potentially non-unitary) matrix G, before closest unitary ?

Also here in line 67 you compute eigenvectors of G that you store in v, but then you overrite vto put the closest unitary of itself, which means that nows the eigvals may not be exactly the ones of eigvals, can this cause a problem?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to e-10 it doesn't make much of a difference, it's ugly and arbitrary but it works well still.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put a TODO or a FIXME in the code, because it can potentially numerically cause some problems, especially for larger matrices

Comment thread mpqp/tools/unitary_decomposition.py Outdated
Comment thread mpqp/translation/braket.py Outdated
Comment thread mpqp/translation/utils.py Outdated
Comment thread mpqp/core/circuit.py Outdated
MathieuG-Colibri and others added 4 commits July 20, 2026 18:19
Co-authored-by: hJaffaliColibritd <133856040+hJaffaliColibritd@users.noreply.github.com>
Co-authored-by: hJaffaliColibritd <133856040+hJaffaliColibritd@users.noreply.github.com>
Co-authored-by: hJaffaliColibritd <133856040+hJaffaliColibritd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants