# H.6.3(b) Hamiltonian subcircuit control

**URL:** https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578
**Category:** Codebook
**Created:** [February 17, 2023, 10:22pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578 "2023-02-17T22:22:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![yan\_c](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/yan_c/32/1487_2.png) [@yan\_c](https://discuss.pennylane.ai/u/yan_c)
#### Post date: [February 17, 2023, 10:22pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578/1 "2023-02-17T22:22:54Z")

</div>

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

 ![image](https://canada1.discourse-cdn.com/flex012/uploads/pennylane/original/2X/d/d035212568128fecbd120946da835bd3b07cb2de.png)

I know there was a question [here](https://discuss.pennylane.ai/t/h-6-3-b-controlled-subcircuit/1497)  
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.

```auto
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])

```

---

<div class="post-metadata">

### Author: ![David\_Wakeham](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/david_wakeham/32/1515_2.png) [@David\_Wakeham](https://discuss.pennylane.ai/u/David_Wakeham)
#### Post date: [February 24, 2023, 9:32pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578/2 "2023-02-24T21:32:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![David\_Wakeham](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/david_wakeham/32/1515_2.png) [@David\_Wakeham](https://discuss.pennylane.ai/u/David_Wakeham)
#### Post date: [February 27, 2023, 4:01pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578/3 "2023-02-27T16:01:41Z")

</div>

_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!

---

<div class="post-metadata">

### Author: ![yan\_c](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/yan_c/32/1487_2.png) [@yan\_c](https://discuss.pennylane.ai/u/yan_c)
#### Post date: [February 27, 2023, 8:14pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578/4 "2023-02-27T20:14:20Z")

</div>

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. 🙂

---

<div class="post-metadata">

### Author: ![David\_Wakeham](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/david_wakeham/32/1515_2.png) [@David\_Wakeham](https://discuss.pennylane.ai/u/David_Wakeham)
#### Post date: [February 27, 2023, 8:33pm UTC](https://discuss.pennylane.ai/t/h-6-3-b-hamiltonian-subcircuit-control/2578/5 "2023-02-27T20:33:25Z")

</div>

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