H.6.3(b) Hamiltonian subcircuit control

Hi!
I have a question on the implementation of the control with 2 auxiliary qubits. The circuit we are asked to implement is this

I know there was a question here
But I can’t figure out why we need to add PauliX gates on wires=0? This is not in the circuit we are asked to implement. My code only succeeds if I add these two gates.

def exp_U_second(U, t):
    """Implement the second-order approximation of exp(tU).
    
    Args:
        U (array): A unitary matrix, stored as a complex array.
        t (float): A time to evolve by.
    """
    
    def subcircuit():
        ##################
        # YOUR CODE HERE #
        ##################
        qml.QubitUnitary(V(t/2), wires=aux[1])
        qml.ControlledQubitUnitary(U@U, control_wires=aux[1], wires=main, control_values='0')
        qml.ControlledQubitUnitary(U, control_wires=aux[1], wires=main, control_values='1')
        qml.QubitUnitary(np.transpose(V(t/2)), wires=aux[1])        
    
    # ADD CONTROLLED OPERATION HERE
    
    qml.QubitUnitary(V(t), wires=aux[0])    
    **qml.PauliX(wires=aux[0])**
    ctrl_fn = qml.ctrl(subcircuit, aux[0], control_values='0')
    ctrl_fn()
    **qml.PauliX(wires=aux[0])**
    qml.QubitUnitary(np.transpose(V(t)), wires=aux[0])

Hi yan_c! Hmm, looks like the control values are mixed up here. The Pauli X will effectively switch to control_values = '1'. I think this is an error in the codercise rather than the diagram! Thanks.

2 Likes

Update: This isn’t a bug after all! If you replace '0' (a string) with 0 (a number), it will work. The issue is that qml.ctrl takes a Bool (or a list of Bools) as control_values, and here it is interpreting '0' as a nonempty string which defaults to 1 in Python. Tricky!

1 Like

Ohhhh! Thanks so much David!!
It looks like qml.ctrl() takes integer values as argument for control_values; while qml.ControlledQubitUnitary takes strings values
Do you think it would make more sense to have the same string values? I’m happy to propose this on github. :slight_smile:

1 Like

Yeah, this is very confusing. If you’re happy to propose this on github go ahead! Otherwise I will :slight_smile: Thanks!

1 Like