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

Update GST to take in gates.Unitary #1587

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
26 changes: 20 additions & 6 deletions src/qibo/tomography/gate_set_tomography.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ def _gate_tomography(
ValueError,
f"Mismatched inputs: nqubits given as {nqubits}. {gate} is a {len(gate.qubits)}-qubit gate.",
)
gate = gate.__class__(*gate.qubits, **gate.init_kwargs)
if gate.__class__.__name__ == "Unitary":
gate = gate.__class__(*gate.init_args)
else:
gate = gate.__class__(*gate.qubits, **gate.init_kwargs)

# GST for empty circuit or with gates
matrix_jk = 1j * np.zeros((4**nqubits, 4**nqubits))
Expand Down Expand Up @@ -230,7 +233,8 @@ def GST(
gate_set (tuple or set or list): set of :class:`qibo.gates.Gate` and parameters to run
GST on.
E.g. gate_set = [(gates.RX, [np.pi/3]), gates.Z, (gates.PRX, [np.pi/2, np.pi/3]),
(gates.GPI, [np.pi/7]), gates.CNOT]
(gates.GPI, [np.pi/7]), gates.CNOT,
(gates.Unitary, (np.array([[1,0],[0,1]])))]
nshots (int, optional): number of shots used in Gate Set Tomography per gate.
Defaults to :math:`10^{4}`.
noise_model (:class:`qibo.noise.NoiseModel`, optional): noise model applied to simulate
Expand Down Expand Up @@ -292,10 +296,17 @@ def GST(

if isinstance(gate, tuple):
angles = ["theta", "phi", "lam"]
matrix = ["unitary"]
gate, params = gate
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in angles]
angle_values = dict(zip(valid_angles, params))
if isinstance(params, list):
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in angles]
angle_values = dict(zip(valid_angles, params))
else:
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in matrix]
angle_values = dict(zip(valid_angles, [params]))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(params, list):
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in angles]
angle_values = dict(zip(valid_angles, params))
else:
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in matrix]
angle_values = dict(zip(valid_angles, [params]))
if not isinstance(params, list):
params = [params]
init_args = signature(gate).parameters
valid_angles = [arg for arg in init_args if arg in angles]
angle_values = dict(zip(valid_angles, params))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @renatomello for spotting this. I have made the changes. But still trying to get it to work. It's currently complaining

FAILED tests/test_tomography_gate_set_tomography.py::test_GST[numpy-False-target_gates0] - ValueError: axes don't match array
FAILED tests/test_tomography_gate_set_tomography.py::test_GST[numpy-True-target_gates0] - ValueError: axes don't match array
FAILED tests/test_tomography_gate_set_tomography.py::test_GST[qibojit-numba-False-target_gates0] - ValueError: axes don't match array
FAILED tests/test_tomography_gate_set_tomography.py::test_GST[qibojit-numba-True-target_gates0] - ValueError: axes don't match array

Will keep trying to debug.


else:
angle_values = {}
init_args = signature(gate).parameters
Expand All @@ -309,7 +320,10 @@ def GST(
RuntimeError,
f"Gate {gate} is not supported for `GST`, only 1- and 2-qubit gates are supported.",
)
gate = gate(*range(nqubits), **angle_values)
if "unitary" in angle_values:
gate = gate(angle_values["unitary"], *range(nqubits))
else:
gate = gate(*range(nqubits), **angle_values)

matrices.append(
_gate_tomography(
Expand Down
1 change: 1 addition & 0 deletions tests/test_tomography_gate_set_tomography.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def test_gate_tomography_noise_model(backend):
gates.SX(0),
gates.RX(0, np.pi / 4),
gates.PRX(0, np.pi, np.pi / 2),
gates.Unitary(np.array([[1, 0], [0, 1]]), 0),
gates.CY(0, 1),
],
[gates.TOFFOLI(0, 1, 2)],
Expand Down
Loading