Skip to content

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented Nov 15, 2021

Finishes off #3728

@dabacon dabacon requested review from cduck, vtomole and a team as code owners November 15, 2021 21:56
@dabacon dabacon requested a review from viathor November 15, 2021 21:56
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Nov 15, 2021
@CirqBot CirqBot added the size: S 10< lines changed <50 label Nov 15, 2021
Copy link
Collaborator

@viathor viathor left a comment

Choose a reason for hiding this comment

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

LGTM modulo one comment and one nit.

'sx': QasmGateStatement(
qasm_gate='sx', num_params=0, num_args=1, cirq_gate=ops.XPowGate(exponent=0.5)
),
'sxdc': QasmGateStatement(
Copy link
Collaborator

Choose a reason for hiding this comment

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

sxdc -> sxdg?

elif self._exponent % 2 == 0.5:
return args.format('sx {0};\n', qubits[0])
elif self._exponent % 2 == 1.5:
return args.format('sxdg {0};\n', qubits[0])
Copy link
Collaborator

@viathor viathor Nov 15, 2021

Choose a reason for hiding this comment

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

There is a general question here: to what extent should _qasm_ be simplifying the circuit using known gate properties like X**2=I? On one hand, we could argue that we should preserve programmer's intention in writing something like rx(2.5pi) on the grounds that perhaps they know some low-level hardware details (e.g. gate timing or noise) and simplification would deny them a useful control knob. On the other hand, we could argue for mathematical simplicity and predictability for example to enable the consumer of the QASM to make some reasonable assumptions about the range of parameters such as rotation angles (e.g. they may expect RX rotation angle to be in say [0, 2pi)).

The mathematician in me prefers the latter, but the hacker wants the former. I could live with either. However, the current code bothers me a little bit since by failing to apply simplification for the case of even integer exponents it does not take a uniform stance on the above question.

Suggestions:

  1. To make the mathematician happy: handle self._exponent % 2 == 0, e.g. by returning empty string (if this is a valid behavior for _qasm_) or explicit identity gate or at least "rx(0)" for all even integer exponents.
  2. To make the hacker happy: remove % 2, i.e. preserve the old treatment of exponents.

My preference is for 2, but I leave the choice up to you.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A variant of option 1 also changes

return args.format('rx({0:half_turns}) {1};\n', self._exponent, qubits[0])

to

return args.format('rx({0:half_turns}) {1};\n', self._exponent % 2, qubits[0])

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Chose 2. I also chose to make it X**(-0.5) as that feels like the most likely use case.

It feels to me like this is one of those places where we should have had a design where the gate stuff sits on one side and the serialization / instruction set stuff sits on the other side, then there might be default way to use gate stuff to do instruction stuff, but this could be mediated by something that can make other decisions.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to X**(-0.5)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I agree it'd be great to have a design clearly stating such things, e.g. adopting or rejecting a principle like

serialization code must be faithful at the parameter level, not merely at the gate algebra level.

As it is, I'm not even sure cirq's behavior is consistent across various gates.

elif self._exponent % 2 == 0.5:
return args.format('sx {0};\n', qubits[0])
elif self._exponent % 2 == 1.5:
return args.format('sxdg {0};\n', qubits[0])
Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to X**(-0.5)

@dabacon dabacon added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Nov 16, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Nov 16, 2021
@CirqBot CirqBot merged commit c15af92 into quantumlib:master Nov 16, 2021
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Nov 16, 2021
@dabacon dabacon deleted the sxdg branch April 16, 2022 15:42
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. size: S 10< lines changed <50
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants