Write code to run the four quantum computations shown below. Explain what you ex
ID: 3862908 • Letter: W
Question
Write code to run the four quantum computations shown below. Explain what you expect to measure. Perform the measurements and reconcile with your expectations. Provide the data in a neat tabular form. You can also include the .m files to show that this works. Those files should be well documented so it is clear what they are supposed to do. Gates are applied to qubits by successively operating on |psi) from the left. Initialize |psi) to 0. The top line is for operations on qubit 1, second line for operations on qubit 2, etc. (a) The operation is H^(2)|psi). (b) The operation is H^(3) H^(2) H^(1)|psi). (c) The operation is H^(3) H^(3)|psi). (d) The operation is H^(3) R^(3)_z H^(3)|psi).Explanation / Answer
#include
#include"expression.h"
#include
#include
#include
using namespace std;
expression::expression() {
// ifix = pfix = "";
ifix = "";
pfix = "";
last = false;
}
bool expression::precedence(char s, char c) const{
if (s == '(' || s == '$') return false;
if(s=='*'|| s =='/') return true;
return (c ='+' || c=='-');
}
void expression::convertToPostFix() {
stack s;
s.push('$');
pfix = "";
for (size_t i = 0; i < ifix.size() - 1; ++i) {
switch (ifix[i]){
case'(': s.push(ifix[i]);
break;
case ')': while (s.top() != '(') {
pfix += s.top();
s.pop();
}
s.pop();
break;
case'+':
case '-':
case '*':
case '/' : while (precedence(s.top(), ifix[i])) {
pfix += s.top();
s.pop();
}
s.push(ifix[i]);
break;
default:
pfix += ifix[i];
}
}
while (s.top() != '$')
pfix += s.top();
}
std::istream& operator>>(std::istream& in, expression& exp){
char sym;
exp.ifix = "";
do {
in >> sym;
exp.ifix += sym;
}
while (sym != '.' && sym != ';');
if (sym == '.') exp.last = true;
exp.convertToPostFix();
return in;
}
std::ostream& operator<<(std::ostream& out, expression& exp) {
out << "Infix: " << exp.ifix << std::endl;
out << "Postfix: " << exp.pfix << std::endl;
return out;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.