-
Notifications
You must be signed in to change notification settings - Fork 61
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
Circuit.unitary
breaks in the presente of two-qubit gate initialized by Gate.controlled_by
#1279
Comments
|
To investigate a bit I made a custom gate by subclassing With this hack I can work around the issue consistently: class FGate(q.gates.Gate):
'''
The F(α, β) gate is an multi-controlled gate defined by:
F(α,β) |1..10> = α|1..10>
F(α,β) |1..11> = β|1..10>
Its matrix representation is diag(1,..,1,α,β).
'''
def __init__(self, diag, target, name='F'):
super().__init__()
self._values = diag
self.unitary = True
self.target_qubits = (target,)
self.name = 'fgate'
self.draw_label = name
controls = range(target)
if controls:
self.control_qubits = tuple(controls)
self.is_controlled_by = True
def matrix(self, backend=None):
import inspect
caller = inspect.stack()[1]
if caller.function == 'matrix_fused':
npad = len(self._values) * (2 ** len(self.control_qubits) - 1)
return np.diag(np.concatenate([np.ones(npad), self._values]))
elif caller.function == 'apply_gate':
return np.diag(self._values) |
This inconsistency was due to these classes defaulting to other gate classes depending on the number of controls added. In your example, |
I can confirm the fix in #1367 works for me. def Fgate(diag, n, name='F'):
'''
The F(α, β) gate is an multi-controlled gate defined by:
F(α,β) |1..10> = α|1..10>
F(α,β) |1..11> = β|1..10>
Its matrix representation is diag(1,..,1,α,β).
'''
gate = q.gates.Unitary(np.diag(diag), n-1, name=name)
gate = gate.controlled_by(*range(n-1))
gate.draw_label = name
return gate |
Code to reproduce bug:
The text was updated successfully, but these errors were encountered: