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

Invalid serialization of DiagonalBox? #45

Closed
aborgna-q opened this issue Jul 4, 2024 · 0 comments · Fixed by #48
Closed

Invalid serialization of DiagonalBox? #45

aborgna-q opened this issue Jul 4, 2024 · 0 comments · Fixed by #48
Assignees
Labels
bug Something isn't working P-high High priority

Comments

@aborgna-q
Copy link
Collaborator

aborgna-q commented Jul 4, 2024

The following pytket/tket2 script (by @johnchildren) shows a mismatched encoding/decoding of diagonal boxes. The bug seems to originate from this crate, but we should write a reduced test case.

import json
from pytket.circuit import DiagonalBox, QControlBox
from pytket import Circuit
import numpy as np
from tket2.circuit import Tk2Circuit
from dictdiffer import diff

u_diagonal = np.array([1, 1, np.e ** (1j * np.pi / 4), np.e ** (1j * np.pi / 8)])


d_box = DiagonalBox(u_diagonal)

controlled_d = QControlBox(d_box, n_controls=1)

circ = Circuit(3).add_gate(controlled_d, [0, 1, 2])

circ.get_unitary()  # This works, circuit is unitary

tket2_circ = Tk2Circuit.from_tket1_json(json.dumps(circ.to_dict()))

# Causes a different dictionary ot be output.
postpostcirc = json.loads(tket2_circ.to_tket1_json())

# Dictionaries are different
print(circ.to_dict())
print(postpostcirc)

print(list(diff(circ.to_dict(), postpostcirc)))

# ValueError: The input diagonal passed to DiagonalBox is not unitary.
Circuit.from_dict(postpostcirc)

pytket circuit json:

{"bits": [], "commands": [{"args": [["q", [0]], ["q", [1]], ["q", [2]]], "op": {"box": {"control_state": 1, "id": "0dadbfea-5391-41c2-ad71-fb01fdaecddd", "n_controls": 1, "op": {"box": {"diagonal": [[[1.0, 0.0]], [[1.0, 0.0]], [[0.7071067811865476, 0.7071067811865475]], [[0.9238795325112867, 0.3826834323650898]]], "id": "919b6c13-b9c4-4ffc-ad1a-660088402d02", "type": "DiagonalBox", "upper_triangle": true}, "type": "DiagonalBox"}, "type": "QControlBox"}, "type": "QControlBox"}}], "created_qubits": [], "discarded_qubits": [], "implicit_permutation": [[["q", [0]], ["q", [0]]], [["q", [1]], ["q", [1]]], [["q", [2]], ["q", [2]]]], "phase": "0.0", "qubits": [["q", [0]], ["q", [1]], ["q", [2]]]}
@aborgna-q aborgna-q added bug Something isn't working P-high High priority labels Jul 4, 2024
@aborgna-q aborgna-q self-assigned this Jul 4, 2024
github-merge-queue bot pushed a commit that referenced this issue Jul 5, 2024
… `f32`s. (#48)

Fixes #45. The elements in the encoded matrix where deserialized into
`f32`, which caused a precision loss.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-high High priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant